DEV Community

Ali Hamza
Ali Hamza

Posted on

Day 174 of Learning MERN Stack

Hello Dev Community! 👋

It is officially Day 174 of my full-stack engineering journey! Today, I explored Union Types, Literal Types, and the dangers of implicit any in TypeScript! 📘⚡

Here is how flexible typing and strict status constraints work under the hood.


🛠️ Technical Breakdown: Unions, Literals & any

As captured in my code editor setup (index.ts):

1. Flexible Union Types & Strict String Literals

  • Used bitwise OR operator syntax (|) to allow multi-type assignments or restrict variables to fixed values:

typescript
  let subs: number | string = 10;
  subs = '10M'; // Valid reassignment

  let apiReq: 'pending' | 'success' | 'error' = 'pending'; // Restricts allowed status values
Enter fullscreen mode Exit fullscreen mode

Top comments (0)