TypeScript Functions and Narrowing/Exhaustive switch with `never`

Catch unhandled union members.

Section: Narrowing and Control Flow

Exhaustive switch with `never`

typescript
typescript
type State = "idle" | "loading" | "success";

function render(state: State) {
  switch (state) {
    case "idle":
      return "Idle";
    case "loading":
      return "Loading";
    case "success":
      return "Done";
    default: {
      const _exhaustive: never = state;
      return _exhaustive;
    }
  }
}
Explanation

The `never` assignment fails if a new union member is added but not handled.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Narrowing and Control Flow
Narrow with `typeof`
Refine primitive unions using runtime checks.
OpenIn sheettypescriptsame section
Narrow with `instanceof`
Refine class-based unions using constructor checks.
OpenIn sheettypescriptsame section
Narrow with the `in` operator
Refine unions by checking property existence.
OpenIn sheettypescriptsame section
Custom type guard with predicate
Teach TypeScript a reusable narrowing rule.
OpenIn sheettypescriptsame section
Type a function
Annotate parameters and return values.
OpenIn sheettypescript1 tag match
Type an async function
Return a typed promise from an async function.
OpenIn sheettypescript1 tag match