ProgramGuru

Exhaustive Checks with never

Understand Exhaustive Checks with never

An exhaustive check uses the never type to make missing union cases visible when a new variant is added.

What you will learn: the purpose of exhaustive checks with never, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how exhaustive checks with never works and where its type rules apply.
  • Predict the exact output of the exhaustive checks with never example.
  • Diagnose a common issue: Do not force a leftover union value to never with a cast; that suppresses precisely the missing-case error you want.

Build the mental model

When every alternative is already handled, the remaining discriminated-union value has type never. Assigning the leftover value to never in a default branch causes a compile error if a future variant is not handled. Use this pattern for evolving event and state models.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

Exhaustive Checks with never: explained visuallyShape variants → Handle each kind → never remainsShape variantsHandle each kindnever remainsFollow the values through the operation
Exhaustive Checks with never: Shape variants → Handle each kind → never remains.

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

type Shape =
  | { kind: "square"; side: number }
  | { kind: "rectangle"; width: number; height: number };
function area(shape: Shape): number {
  switch (shape.kind) {
    case "square": return shape.side ** 2;
    case "rectangle": return shape.width * shape.height;
    default: {
      const unreachable: never = shape;
      return unreachable;
    }
  }
}
console.log(area({ kind: "square", side: 4 }));
console.log(area({ kind: "rectangle", width: 3, height: 5 }));

Verified output

16
15

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

In the square branch, 4 squared is 16. In the rectangle branch, 3 times 5 is 15. Both variants return before default, so the assignment to never is type-correct for the declared union.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

Do not force a leftover union value to never with a cast; that suppresses precisely the missing-case error you want.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Add a circle variant to Shape without editing the switch. Inspect the compiler error, then add the missing branch.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

What happens after adding an unhandled variant to an exhaustive switch?
Choose one answer. You can try again.

Answer and explanation

The never assignment reports a type error until the new case is handled.

An exhaustive check uses the never type to make missing union cases visible when a new variant is added.

Review the key idea

Exhaustive Checks with never: An exhaustive check uses the never type to make missing union cases visible when a new variant is added.

In the square branch, 4 squared is 16. In the rectangle branch, 3 times 5 is 15. Both variants return before default, so the assignment to never is type-correct for the declared union.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Introduction to Classes →