On this page
never
Prev: `unknown` | Up: builtins | Next: `void`
Rel: builtins, Structural Typing & Narrowing
Ref: TypeScript Docs — never
Tags: #public #web #typescript #builtins #tier/core
Syntax & Overview
function fail(msg: string): never { throw new Error(msg); }
Represents values that can never occur (functions that always throw/loop forever, or impossible branches after exhaustive narrowing).
Notes & Pitfalls
- Bottom type (no value can ever have type
never): Ideal for compile-time exhaustiveness checks inswitchstatements over discriminated unions.
Example
type Shape = { kind: "circle"; r: number } | { kind: "square"; s: number };
function area(shape: Shape): number {
switch (shape.kind) {
case "circle": return Math.PI * shape.r ** 2;
case "square": return shape.s ** 2;
default: {
const _exhaustive: never = shape;
return _exhaustive;
}
}
}
Linked from
Related notes
- tuts/typescript/builtins/unknown Linked here; links to this page; shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/void Linked here; links to this page; shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins Linked here; links to this page; shared tags: #builtins, #typescript, #web
- tuts/typescript/type_system/structural_typing_and_narrowing Linked here; links to this page; shared tags: #typescript, #web
- tuts/typescript/builtins/any shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/as_const shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/asserts shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/enum shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/infer shared tags: #builtins, #tier, #typescript, #web
- tuts/typescript/builtins/is shared tags: #builtins, #tier, #typescript, #web