Docs
/
TypeScript
Chapter 5
05 — Union & Intersection Types
Core Concepts
- Union Types (
A | B) — value can be A or B (or both) - Intersection Types (
A & B) — value must be both A and B - Narrowing — reducing a union to a specific type via checks
- Literal Unions — specific values as types (
"a" | "b" | "c") - Discriminated Unions — unions with a shared
kind/typefield for safe narrowing - Distributive Behavior — how unions interact with generics and conditionals
Union vs Intersection
| Feature | Union (A | B) | Intersection (A & B) |
|---|---|---|
| Meaning | A or B | A and B |
| Properties accessible | Only shared ones | All from both |
| Use case | Multiple possible types | Combining types |
| Narrowing needed | Yes | No |