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
Top comments (0)