← TypeScript learning pathTypeScript · Module 5 of 10
Unions, literals and narrowing
Combine types and let TypeScript narrow them safely.
📖 Key ideas
- Union: let id: string | number.
- Literal types: type Size = "S" | "M" | "L".
- Narrow with typeof, instanceof, in or equality checks.
- Discriminated unions use a shared kind field.
- Optional chaining ?. and nullish ?? handle missing values.
💡 Real-life example: A union is a parking spot marked "car or bike": check which one arrived before you lock it.
UnionValue can be one of several types
Literal typeExact value as a type
NarrowingRefining a type with checks
Discriminated unionUnion with a common tag field