Unions for TypeScripts
Unions let a value be one of several types, written with the pipe symbol, and literal types restrict a value to exact strings or numbers. Narrowing is how TypeScript refines a union inside a branch, using typeof, in, instanceof, equality checks and discriminated unions. Together they model states precisely and make impossible states unrepresentable.
Stage: Type Basics · Part of the TypeScript roadmap · Last reviewed 19 November 2026
What to learn in Unions
- Declare a union type and a string literal type for allowed values
- Narrow a union with typeof, in and instanceof checks
- Model states with a discriminated union and a shared kind field
- Use exhaustive checking with never so a new case causes an error
- Write a custom type guard function with a type predicate
Unions interview questions
What is a discriminated union?
It is a union of object types that share a common literal property, such as kind: 'circle' or kind: 'square'. Checking that property narrows the value to one member, so each branch can safely access that type's fields. It is a common way to model states and results.
How do you check a switch is exhaustive in TypeScript?
In the default branch, assign the value to a variable of type never. If a union member is not handled, the value is not never and the compiler reports an error. This makes adding a new union case flag every place that needs updating.
Where Unions fits in the TypeScript roadmap
Unions belongs to the Type Basics stage (beginner level), which takes about 17 hours. Describe your data.
Other units in this stage
Back to the full TypeScript roadmap