Skip to main content

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

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.

Back to the full TypeScript roadmap