never (type)
The TypeScript type with no values, used for code that can never be reached or functions that never return.
In more detail
A function that always throws returns never. After a switch has handled every member of a union, the remaining value is never, which is how exhaustiveness checks work. Intersecting incompatible types, and remapping a key to never in a mapped type, also produce it.
Related terms
- bottom type — A type that no value belongs to, so it fits into every other type; in TypeScript this is never.
- exhaustiveness checking — Having the compiler prove that code handles every possible member of a union, so a newly added member causes a compile error.
- 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.
- intersection type — A type written A & B that requires a value to satisfy both types at once, combining their properties.
Taught in
- Type Annotations, Type Inference And Contextual Typing — Production TypeScript
- Primitive Types: String, Number, Boolean, Bigint And Symbol — Production TypeScript
- Literal Types: String, Number, Boolean Literals And const — Production TypeScript
- any Versus unknown: Defensive Typing In Practice — Production TypeScript
- void, undefined And null: Exact Optional Property Types — Production TypeScript
- never: The Bottom Type And Exhaustive Switch Checking — Production TypeScript
- Optional Properties, Readonly And Index Signatures — Production TypeScript
- Union Types, Intersections And Property Conflicts — Production TypeScript
- Assertion Signatures: asserts condition And Invariants — Production TypeScript
- Exhaustive Narrowing With never In Production Handlers — Production TypeScript
- Mapped Types, Key Remapping With as And Template Literals — Production TypeScript