exhaustiveness checking
Having the compiler prove that code handles every possible member of a union, so a newly added member causes a compile error.
In more detail
The usual technique assigns the value left over in a default branch to never, directly or through assertNever. This turns forgetting a case from a silent run time bug into a build failure that points to the right place.
Related terms
- assertNever — A small helper function taking a parameter of type never, called in the default branch of a switch to prove every case was handled.
- never (type) — The TypeScript type with no values, used for code that can never be reached or functions that never return.
- discriminated union — A type made of several object shapes that share one literal field, such as status or type, whose value tells them apart.
- switch statement — A statement that compares one value against a list of cases and runs the matching branch, with a default for everything else.
Taught in
- void, undefined And null: Exact Optional Property Types — Production TypeScript
- never: The Bottom Type And Exhaustive Switch Checking — Production TypeScript
- Discriminated Unions: Tagged Variants And Pattern Matching — Production TypeScript
- Assertion Signatures: asserts condition And Invariants — Production TypeScript
- Exhaustive Narrowing With never In Production Handlers — Production TypeScript
- The Spine Artifact: Generating Typed Models From OpenAPI — Production TypeScript