Sub topic 9
Narrowing and Exhaustive Checks with `never`
This chapter assumes you know basic TypeScript types, functions, object literals, and switch statements. It also assumes you have seen union types. A union type allows a value to belong to one of several types.
A literal union is a union whose members are exact values:
type Command = "start" | "stop" | "pause";A variable of type Command can contain only one of those three strings. TypeScript can use that information to narrow the variable inside conditional branches.
How narrowing works
Narrowing means reducing a broad type to a more specific type based on runtime checks. TypeScript understands checks such as typeof, equality comparisons, and property checks.
function formatValue(value: string | number): string {
if (typeof value === "string") {
return value.toUpperCase();
}
return value.toFixed(2);
}Before the if, value is string | number. Inside the first branch, TypeScript knows that value is a string. After the branch, it knows that the remaining possibility is number.
A common and powerful pattern uses a discriminated union. Each object variant has a shared property, called a discriminant, with a different literal value.
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; size: number }
| { kind: "rectangle"; width: number; height: number };Checking shape.kind narrows the whole object, so each branch receives the properties that belong to that variant.
Proving that every case is handled
When a union changes over time, a switch can silently become incomplete. For example, if you add "triangle" to Shape but forget to add a branch, the function might compile if its return type allows the missing path.
The never type represents a value that can never occur. In a final branch, assigning the remaining value to never asks the compiler to prove that no union member remains.
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; size: number }
| { kind: "rectangle"; width: number; height: number };
function area(shape: Shape): number {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.size ** 2;
case "rectangle":
return shape.width * shape.height;
default: {
const unreachable: never = shape;
return unreachable;
}
}
}After the three cases, TypeScript sees that no member of Shape remains. Therefore, shape has type never in the default branch.
If you add a triangle without adding a case, the assignment fails:
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; size: number }
| { kind: "rectangle"; width: number; height: number }
| { kind: "triangle"; base: number; height: number };The compiler reports that the triangle variant cannot be assigned to never. The error points to the missing handling.
> callout note > Keep the never assignment inside the default branch. It turns future changes to the union into compile-time errors instead of allowing incomplete behavior to go unnoticed.
Common errors
Do not confuse never with void or unknown.
voidusually describes a function that returns no useful value.unknowndescribes a value whose type is not known yet.neverdescribes a value that cannot exist on a reachable path.
A default branch that merely throws can also enforce runtime behavior, but it does not always give the compiler a useful check about the union:
function area(shape: Shape): number {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.size ** 2;
case "rectangle":
return shape.width * shape.height;
default:
throw new Error(`Unhandled shape: ${shape.kind}`);
}
}This is useful when unexpected data can arrive at runtime. The explicit never assignment is better when the main goal is to detect missing compile-time cases. You can combine both:
function assertNever(value: never): never {
throw new Error(`Unhandled value: ${JSON.stringify(value)}`);
}Then call assertNever(shape) in the default branch. The parameter type enforces exhaustiveness, and the thrown error protects the running program if untyped data reaches it.
Exercise
What compile-time error should appear if a new "triangle" variant is added to Shape but no case "triangle" branch is added?
Show answer
The remaining shape value is a triangle, so it cannot be assigned to never. The compiler reports that the switch is not exhaustive.