← TypeScript learning path
TypeScript · Module 5 of 10

Unions, literals and narrowing

Combine types and let TypeScript narrow them safely.

📖 Key ideas

  1. Union: let id: string | number.
  2. Literal types: type Size = "S" | "M" | "L".
  3. Narrow with typeof, instanceof, in or equality checks.
  4. Discriminated unions use a shared kind field.
  5. 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