Sub topic 11
Exhaustive Checks with `assertNever`
Assume you know TypeScript type annotations, interfaces, union types, and switch statements. You should also know that never represents a value that cannot exist.
When a value can have several related shapes, TypeScript can require your code to handle every shape. This is an exhaustive check. Exhaustive checks are useful when a missing case would produce incorrect behavior.
Discriminated unions
A discriminated union is a union whose members have a shared property with different literal values. The shared property is called the discriminant.
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; size: number }
| { kind: "triangle"; base: number; height: number };The kind property tells TypeScript which member is present. A switch on kind narrows shape inside each case.
The `assertNever` pattern
Define a helper that accepts only never:
function assertNever(value: never): never {
throw new Error(`Unexpected shape: ${JSON.stringify(value)}`);
}The function both documents an impossible path and throws if invalid JavaScript reaches that path at runtime. Its parameter type is the important part. If the compiler believes that a value still has a possible type, it will reject the call.
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; size: number }
| { kind: "triangle"; base: number; height: number };
function assertNever(value: never): never {
throw new Error(`Unexpected shape: ${JSON.stringify(value)}`);
}
function area(shape: Shape): number {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.size ** 2;
case "triangle":
return (shape.base * shape.height) / 2;
default:
return assertNever(shape);
}
}
console.log(area({ kind: "triangle", base: 10, height: 4 }));After the three cases, no member of Shape remains. Therefore, TypeScript narrows shape to never in the default branch, and the call is valid.
How the check catches changes
Suppose you add this member:
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; size: number }
| { kind: "triangle"; base: number; height: number }
| { kind: "rectangle"; width: number; height: number };If you do not add a case "rectangle" branch, shape in the default branch has the type { kind: "rectangle"; width: number; height: number }. That type is not assignable to never, so the compiler reports an error at assertNever(shape).
This makes the missing behavior visible during development instead of allowing it to become a silent runtime bug.
> callout tip > Put the assertNever call in the default branch of a switch over the discriminant. Do not use a plain default branch that returns an ordinary fallback value, because that can hide newly added union members.
Common errors
A frequent mistake is using a broad discriminant type:
type BadShape = {
kind: string;
radius?: number;
size?: number;
};This type does not describe a fixed set of cases. TypeScript cannot prove that the switch handles every possible string. Use literal members in a union instead.
Another mistake is accepting unknown or any in assertNever. The helper must accept never; otherwise, the compiler loses the exhaustiveness check.
assertNever checks the type system, but external data can still be invalid at runtime. Validate JSON or API data before treating it as a trusted Shape.
When to use it
Use exhaustive checks when:
- A union represents all supported commands, events, states, or variants.
- Adding a new variant should require changes in every relevant handler.
- A missing branch could produce incorrect results.
- You want refactoring errors at compile time.
Exercise
Why does assertNever(shape) fail to compile when a new union member has no matching case?
Show answer
The unhandled member remains in the type of shape in the default branch. That type is not assignable to never, so the compiler identifies the missing case.