Exhaustiveness Checking With never
Make the compiler tell you when a switch forgets a case. Learn the never type, the assertNever helper, exhaustive records and what happens at runtime.

Adding a variant to a union is easy. Finding every place that has to handle it is the hard part. A switch that covered three shapes yesterday silently ignores the fourth today, and the bug shows up as a wrong value in production. TypeScript can turn that silent gap into a compile error, using the never type, the type with no values. If a branch is reached only when every member of the union has been handled, the variable in it has type never, and assigning it to anything of a real type is the check. This post shows the pattern step by step, the compiler messages you will see, the assertNever helper, the record-based variant, and the runtime side that the compiler cannot cover. It uses TypeScript 6.x, and the type is described in the handbook section on the never type.
What never means in narrowing
Narrowing subtracts types. Each case in a switch over a discriminated union removes one member from what the variable could still be. When all members have been removed, nothing is left, and the compiler gives the variable the type never.
type Circle = { kind: "circle"; radius: number };
type Square = { kind: "square"; side: number };
type Triangle = { kind: "triangle"; base: number; height: number };
type Shape = Circle | Square | Triangle;
function area(shape: Shape): number {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.side ** 2;
default: {
const unreachable: never = shape;
return unreachable;
}
}
}area.ts(13,13): error TS2322: Type 'Triangle' is not assignable to type 'never'.The triangle case is missing, so in the default branch shape is still Triangle. A value of type Triangle cannot be assigned to never, and the error names the member you forgot. That is the whole mechanism. When you add the case, shape in the default branch becomes never, and the assignment compiles.
type Circle = { kind: "circle"; radius: number };
type Square = { kind: "square"; side: number };
type Triangle = { kind: "triangle"; base: number; height: number };
type Shape = Circle | Square | Triangle;
function area(shape: Shape): number {
switch (shape.kind) {
case "circle":
return Math.PI * shape.radius ** 2;
case "square":
return shape.side ** 2;
case "triangle":
return (shape.base * shape.height) / 2;
default: {
const unreachable: never = shape;
return unreachable;
}
}
}
console.log(area({ kind: "triangle", base: 4, height: 5 }));
console.log(area({ kind: "square", side: 3 }));10
9The check costs one branch. Its value is not at the time you write the function, when you know the cases, but months later when someone edits the union in another file. The compiler reports the error in area, even though area was not touched.
What happens without the check
The compiler gives a weaker form of the same protection when a function has a declared return type and no default.
type Circle = { kind: "circle"; radius: number };
type Square = { kind: "square"; side: number };
type Shape = Circle | Square;
function perimeter(shape: Shape): number {
switch (shape.kind) {
case "circle":
return 2 * Math.PI * shape.radius;
}
}implicit.ts(5,35): error TS2366: Function lacks ending return statement and return type does not include 'undefined'.This catches a missing case when every case returns, which is a common situation. It breaks down when the switch only performs side effects or assigns to a variable, because then nothing has to be returned and the compiler has nothing to complain about. The never assignment works in both situations, so it is the form to use when the function returns nothing, or when you want the error to name the missing member.
The assertNever helper
Writing a const unreachable: never in each switch is noisy. The usual refactor is a small function whose parameter is never, which both holds the type check and gives you a runtime fallback.
export function assertNever(value: never): never {
throw new Error(`Unexpected value: ${JSON.stringify(value)}`);
}Use it as the whole body of the default branch.
import { assertNever } from "./assert-never.js";
type Status = "draft" | "review" | "published";
function label(status: Status): string {
switch (status) {
case "draft":
return "Draft";
case "review":
return "In review";
case "published":
return "Live";
default:
return assertNever(status);
}
}
console.log(label("draft"));
console.log(label("published"));Draft
LiveThis version works on a union of string literals as well, since a switch narrows those directly. If a case is missing, as in the next version, the call is reported at its argument.
import { assertNever } from "./assert-never.js";
type Status = "draft" | "review" | "published";
function label(status: Status): string {
switch (status) {
case "draft":
return "Draft";
case "review":
return "In review";
default:
return assertNever(status);
}
}label-missing.ts(12,26): error TS2345: Argument of type '"published"' is not assignable to parameter of type 'never'.The published case is left out on purpose, and the message names the member that was forgotten.
The runtime half
The type check is a promise about values that obey the types. Data that crosses a boundary, such as JSON from a server written against an older or newer contract, can carry a value the union does not contain. That is why assertNever throws instead of returning something. If the unexpected value arrives, the function fails loudly at the place where the problem is visible.
import { assertNever } from "./assert-never.js";
type Status = "draft" | "review";
function label(status: Status): string {
switch (status) {
case "draft":
return "Draft";
case "review":
return "In review";
default:
return assertNever(status);
}
}
const fromServer = JSON.parse('{"status":"archived"}') as { status: Status };
try {
console.log(label(fromServer.status));
} catch (error) {
if (error instanceof Error) {
console.log(error.message);
}
}Unexpected value: "archived"The cast is where the type and the data parted ways, and the exception is where it became visible. Without assertNever, a default that returned an empty string would hide the contract change until someone read a blank label in the interface. Choose whether an unknown value should crash or degrade on purpose. For a rendering function, a fallback may be kinder. For a payment state machine, an exception is the safer response.
Exhaustive tables with Record
A switch is not the only way to demand a handler for every case. A Record keyed by the union forces an entry for each member.
type Status = "draft" | "review" | "published";
const labels: Record<Status, string> = {
draft: "Draft",
review: "In review",
};record.ts(3,7): error TS2741: Property 'published' is missing in type '{ draft: string; review: string; }' but required in type 'Record<Status, string>'.With a complete table, the lookup is also shorter than the switch, and adding a status turns every table keyed by it into an error until it is filled. Prefer a record when each case maps a key to a value. Keep the switch for cases that run different code.
A related trick uses satisfies, which keeps the precise literal types of the table while still demanding every key: const labels = { ... } satisfies Record<Status, string>. The satisfies operator arrived in TypeScript 4.9.
Exhaustiveness in if chains
The same subtraction applies to if and else if chains, and the final else is where the never check goes.
import { assertNever } from "./assert-never.js";
type Event =
| { type: "click"; x: number; y: number }
| { type: "key"; key: string };
function describe(event: Event): string {
if (event.type === "click") {
return `click at ${event.x},${event.y}`;
} else if (event.type === "key") {
return `key ${event.key}`;
} else {
return assertNever(event);
}
}
console.log(describe({ type: "click", x: 3, y: 9 }));
console.log(describe({ type: "key", key: "Enter" }));click at 3,9
key EnterNote that the compiler does not need assertNever for this function to compile today. It adds protection for the future, when Event grows a third member.
Pitfalls
A switch over a type that is not a closed union cannot be exhaustive. If status is a plain string, the default branch holds string, and assertNever(status) fails to compile. That is correct, since a string has infinitely many values, but the error can look like a bug in the helper. The fix is a better type for the input, usually a union of literals.
When an assignment to never fails where the union looks complete, hover the variable and read what the compiler thinks is left. It is almost always an extra member you did not intend, such as undefined from an optional field.
Finally, a default that returns a harmless value defeats the purpose. A reviewer should treat default: return null in a switch over a union as a hole in the safety net, and ask whether assertNever fits.
Testing the pattern
The compile-time half is tested by the build. The runtime half is worth one case, because a thrown error is behaviour you promised.
import { throws } from "node:assert/strict";
import { assertNever } from "./assert-never.js";
throws(() => assertNever("archived" as never), {
message: 'Unexpected value: "archived"',
});
console.log("assertNever throws on unexpected input");assertNever throws on unexpected inputFor the compile-time half, keep the check where it runs on every change: the type check in continuous integration. A new member in a union becomes a failing build for every switch that has to change, which is the purpose of the whole exercise.


