Course contentsShow
← TypeScript
Lesson 954 of 1,79622. The never TypeFree lesson

What Is an Exhaustive Check?

Understanding how exhaustive checks ensure all cases in a union are handled at compile time.

What Is an Exhaustive Check?

You'll learn: How TypeScript's never type helps you write switches and conditionals that the compiler guarantees handle every possible case.

The Core Idea

An exhaustive check is a compile-time pattern that ensures you've handled all members of a union type. When TypeScript's control flow analysis narrows a union down through all possible branches, what remains should be never—the impossible type. If you forget a case, TypeScript will catch it at compile time.

Think of it like a checklist: if you've crossed off every item, nothing should be left. If something is left, you forgot a step.

How It Works

When you handle each variant of a discriminated union in separate branches (like a switch statement), TypeScript eliminates possibilities one by one. After the last case, the type is narrowed to never. You can assert this with a final branch that expects never:

type Status = "pending" | "approved" | "rejected";

function handleStatus(status: Status): string {
  switch (status) {
    case "pending":
      return "Waiting...";
    case "approved":
      return "Success!";
    case "rejected":
      return "Failed.";
    default:
      // status is now type 'never' here
      const _exhaustive: never = status;
      return _exhaustive;
  }
}

If you later add "cancelled" to the Status union but forget to add a case, TypeScript errors in the default branch because status is no longer never—it's "cancelled".

Why This Matters

Exhaustive checks prevent silent bugs when types evolve. You get a compile-time guarantee that adding a new variant will break your build until you handle it everywhere. This transforms runtime crashes into build failures you can fix before shipping.

Key Takeaway: An exhaustive check uses the never type to verify at compile time that all possible union cases have been handled, catching missing branches before they cause runtime errors.