Sub topic 7
Exhaustiveness Checking with `never`
This chapter assumes you know TypeScript unions, literal types, object types, functions, and if and switch statements. It also assumes that strict mode is enabled in tsconfig.json.
A discriminated union describes one value that can have several possible shapes. Each shape has a discriminant, which is a property with a literal type such as "circle" or "square". TypeScript uses that property to narrow the union.
Why missing cases are dangerous
Consider this union:
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; size: number }
| { kind: "rectangle"; width: number; height: number };A function can handle each current member:
function perimeter(shape: Shape): number {
switch (shape.kind) {
case "circle":
return 2 * Math.PI * shape.radius;
case "square":
return 4 * shape.size;
case "rectangle":
return 2 * (shape.width + shape.height);
}
return 0;
}The final return 0 hides a problem. If someone adds a "triangle" member later, the function still compiles and returns 0 for triangles. The compiler should report that the new case is missing.
Using `never` to prove completeness
never is the type of a value that cannot exist. After a switch handles every member of a discriminated union, no value should remain. TypeScript therefore treats the remaining value as never.
Create a helper that accepts only never:
function assertNever(value: never): never {
throw new Error(`Unexpected shape: ${JSON.stringify(value)}`);
}
function perimeter(shape: Shape): number {
switch (shape.kind) {
case "circle":
return 2 * Math.PI * shape.radius;
case "square":
return 4 * shape.size;
case "rectangle":
return 2 * (shape.width + shape.height);
default:
return assertNever(shape);
}
}The default branch is reachable at runtime if invalid data enters the program, so the helper throws an error. At compile time, the important detail is that shape must be never.
If you add a triangle without adding a case:
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; size: number }
| { kind: "rectangle"; width: number; height: number }
| { kind: "triangle"; base: number; height: number };TypeScript reports an error at assertNever(shape). The value is now a triangle, not never. This turns an overlooked variant into a compile-time failure.
How the check works
The same idea works with conditional statements:
function describe(shape: Shape): string {
if (shape.kind === "circle") {
return `Circle with radius ${shape.radius}`;
}
if (shape.kind === "square") {
return `Square with side ${shape.size}`;
}
if (shape.kind === "rectangle") {
return `Rectangle ${shape.width} by ${shape.height}`;
}
return assertNever(shape);
}Use exhaustive checks when every variant requires deliberate handling. They are especially useful for reducers, message handlers, parsers, and application state. When the union changes, the compiler identifies every function that needs updating.
Exercise
What compile-time error should appear if { kind: "triangle"; base: number; height: number } is added to Shape but no triangle case is added to perimeter?
Show answer
shape in the default branch is inferred as the triangle member, so it cannot be passed to assertNever, which requires a value of type never.