Sub topic 9
Exhaustiveness Checking with `never`
Assume you know basic TypeScript syntax, union types, switch statements, and control-flow narrowing. This chapter focuses on ensuring that code handles every member of a union.
A union can describe a fixed set of alternatives:
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; size: number };The kind property is the discriminant. TypeScript uses its literal values to narrow shape inside each switch case.
Why exhaustive handling matters
An exhaustive function handles every possible member of a union. The compiler can check this for you, which protects code from becoming incomplete when the union changes.
function area(shape: Shape): number {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.size ** 2;
}
}This function currently handles both variants. However, if someone adds a triangle later, the function may stop being correct:
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; size: number }
| { kind: "triangle"; base: number; height: number };The compiler may report that area does not return a value for every path when strict checking is enabled. Even when it reports an error, the location may not clearly identify which case is missing.
The `never` type
never is the type for values that cannot exist. A variable narrowed to never represents a state that TypeScript believes is unreachable.
Add a default branch and assign the remaining value to never:
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; size: number };
function area(shape: Shape): number {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.size ** 2;
default: {
const unreachable: never = shape;
return unreachable;
}
}
}After the circle and square cases, TypeScript knows that no Shape member remains. Therefore, shape has type never in the default branch.
If you add triangle to Shape but do not add a case, shape becomes a triangle in the default branch. Assigning it to never produces a compile-time error. The error points to the exhaustive check.
Reusing an assertion helper
A helper function can make the pattern shorter and provide a useful runtime error if an unexpected value reaches the branch:
function assertNever(value: never): never {
throw new Error(`Unexpected value: ${JSON.stringify(value)}`);
}
type Result =
| { status: "success"; value: string }
| { status: "failure"; error: string };
function describe(result: Result): string {
switch (result.status) {
case "success":
return `Value: ${result.value}`;
case "failure":
return `Error: ${result.error}`;
default:
return assertNever(result);
}
}The parameter type never requires callers to pass an impossible value. If a new Result variant is added without a corresponding case, the call assertNever(result) fails to compile.
The function also throws at runtime. This matters when JavaScript calls the function, when data comes from an untyped source, or when a value bypasses TypeScript's checks.
Common mistakes
- Do not use
defaultas a silent fallback if every variant requires deliberate handling. - Do not cast the value to
never. A cast hides the missing case instead of checking it. - Keep the discriminant required and stable. Optional or broadly typed discriminants weaken narrowing.
- Keep each case focused on one variant so the compiler can expose the correct properties.
Exercise
What compile-time failure should occur if a new "triangle" member is added to Shape but no case "triangle" is added to area?
Show answer
The value in the default branch is no longer never, so assigning it to never or passing it to assertNever produces a type error.