Exhaustiveness Checking
Exhaustiveness checking is making the compiler prove that you handled every member of a union. Add a new case to the union and the build fails in each place that forgot it, instead of failing in production.
Detailed Explanation
Handling a fixed set of cases is easy. Keeping that handling correct six months later, after somebody adds a seventh notification channel, is the hard part. Without help, the new channel falls through to a default branch or returns undefined, and nobody finds out until a user does.
The trick relies on never. When you narrow a union and eliminate every member, the remaining type is never — the type with no values. So in the default branch of a switch over a discriminated union, if you handled all cases, the value has type never. If you missed one, the value still has that missed member's type. Assigning it to something typed never then fails, and the error message names exactly what you forgot.
The idiomatic form is a small helper:
function assertNever(value: never): never {
throw new Error(`Unhandled case: ${JSON.stringify(value)}`);
}
Call it from default. It costs one function and one line per switch, and it turns a silent gap into a compile error. It also throws at runtime, which matters when malformed data arrives from outside the type system and the value really is something you never modelled.
There is a lighter version that needs no helper at all: annotate the return type and return from every branch. If the function promises string and one case is unhandled, control reaches the end of the function and the compiler reports "Function lacks ending return statement". This works because there is no implicit undefined to fall back on. It depends on strictNullChecks being on, or on noImplicitReturns. It is the cheapest exhaustiveness check available, and it is why so much TypeScript is written as a switch where every arm returns.
Two compiler flags back this up:
noImplicitReturns— errors when some code paths return a value and others fall off the end. This catches the missed case even when the function has no return type annotation.noFallthroughCasesInSwitch— errors when a non-emptycaseclause runs into the next one withoutbreak,return, orthrow. It does not check exhaustiveness, but it removes the other classicswitchbug. Stacked empty cases (case "a": case "b": ...) are still allowed.
Neither flag is included in strict, so you have to turn them on yourself.
What it costs: a helper import in every file that switches, and slightly noisier code. What it buys is the property that makes union modelling worth doing at all — the compiler, not your memory, becomes the thing that tracks where each case is handled.
Code Examples
1. The never-assertion helper in a default branch.
// 1. The never-assertion helper in a default branch.
type Channel =
| { kind: "email"; address: string }
| { kind: "sms"; number: string };
function assertNever(value: never): never {
throw new Error(`Unhandled case: ${JSON.stringify(value)}`);
}
function send(msg: Channel): string {
switch (msg.kind) {
case "email":
return `mailto:${msg.address}`;
case "sms":
return `sms:${msg.number}`;
default:
return assertNever(msg); // msg is 'never' here, because both cases are handled
}
}2. Add a case to the union and the compiler points at the switch.
// 2. Add a case to the union and the compiler points at the switch.
type Channel =
| { kind: "email"; address: string }
| { kind: "sms"; number: string }
| { kind: "push"; deviceId: string }; // new member
function send(msg: Channel): string {
switch (msg.kind) {
case "email":
return `mailto:${msg.address}`;
case "sms":
return `sms:${msg.number}`;
default:
return assertNever(msg);
// Error: Argument of type '{ kind: "push"; deviceId: string; }'
// is not assignable to parameter of type 'never'.
}
}3. No helper: an annotated return type does the checking for you.
// 3. No helper: an annotated return type does the checking for you.
type Level = "debug" | "info" | "error";
function colorFor(level: Level): string {
switch (level) {
case "debug":
return "grey";
case "info":
return "blue";
// "error" is not handled
}
// Error: Function lacks ending return statement and return type
// does not include 'undefined'. (requires strictNullChecks)
}4. The flags that make the checks reliable. Neither is part of "strict".
// 4. The flags that make the checks reliable. Neither is part of "strict".
{
"compilerOptions": {
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
}
}Common Gotchas
An unnecessary `default` can hide the gap
a `default` that returns a sensible fallback value makes every union member "handled", so adding a case produces no error at all. Either assert `never` in `default`, or omit `default` and let the return type catch it.
Exhaustiveness is compile-time only
the check proves your source covers the declared union. If untyped JSON arrives with `kind: "fax"`, the `default` branch still runs at runtime, which is why `assertNever` should throw rather than return silently.
`noFallthroughCasesInSwitch` is not an exhaustiveness check
it only reports a case body that runs into the next case. You still need the `never` assertion or a returning `switch` to catch missing members.