never type
๐ What is the never Type in TypeScript? The never type represents a value that can NEVER exist -- code that is unreachable. A function that always throws an error never returns a value, so its return type is never. The most powerful use of never is in exhaustive type checking: if you handle every โฆ

Appy Saysโฆ
never is TypeScript's most mysterious type. A value of type never literally cannot exist. But that makes it powerful: it's how TypeScript proves your code handles every possible case, leaving nothing unhandled.
What is the never type?
The never type represents values that can never occur. It's the 'bottom type' โ no value is assignable to never, but never is assignable to everything.
- โขFunctions that always throw: return type is never
- โขFunctions that never return (infinite loops): return type is never
- โขAfter exhausting all union members in a type guard, the remaining type is never
- โขnever is assignable to every type (it never actually produces a value)
- โขNothing (except never itself) is assignable to never
- โข
string | neversimplifies tostringโ never contributes nothing to a union
Think of it like an impossible Minecraft crafting recipe
Some crafting recipe combinations can never produce an item โ they're structurally impossible. never is the type for those impossible values. If TypeScript infers never, it's telling you: 'this code path cannot happen โ you've covered every case'.
How It Works
- โข1. Function that always throws:
function fail(msg: string): never { throw new Error(msg); } - โข2. Exhaustive check pattern: after handling all union cases, pass remainder to a never parameter
- โข3.
function assertNever(x: never): never { throw new Error('Unhandled case: ' + x); } - โข4. In a switch over
type Shape = Circle | Square | Triangle: - โข5. After
case 'circle',case 'square',case 'triangle': - โข6.
default: assertNever(shape)โ if you add a new shape and forget a case, it errors here
Real-World Examples
- โข
function exhaustiveCheck(x: never): never { throw new Error('Missed case: ' + JSON.stringify(x)); } - โขError handler:
function panic(msg: string): never { throw new Error(msg); } - โขIn discriminated union switch โ default: assertNever(action)
- โขConditional type bottoms:
T extends string ? T : neverโ filters to only string subtypes
Key Facts
- โขnever is the TypeScript equivalent of the mathematical empty set โ โ
- โขConditional types use never as the 'not matching' branch:
T extends U ? X : never - โขMapped types use never to exclude properties: filters where result is never get removed
- โขThe Exclude utility type uses never:
type Exclude<T, U> = T extends U ? never : T;
Watch Out!
If TypeScript infers a variable's type as never somewhere unexpected, it means you've already narrowed all possible types and code beyond that point is unreachable. Don't cast away the never โ investigate why TypeScript thinks the code is unreachable.
Remember
never = impossible to have a value. Use it for always-throws functions and exhaustive switch defaults. The assertNever pattern ensures you handle every case of a union โ add a new case later and TypeScript catches the forgotten branch.
What You Learned
- โขnever: values that cannot exist โ bottom type; only functions that throw/loop forever return it
- โขExhaustive never pattern: assertNever in switch default catches unhandled union members
- โขUnlocks: bulletproof discriminated unions, complete switch coverage, conditional type filtering
Key Facts
- โnever is the TypeScript equivalent of the mathematical empty set โ โ
- โConditional types use never as the 'not matching' branch:
T extends U ? X : never - โMapped types use never to exclude properties: filters where result is never get removed
- โThe Exclude utility type uses never:
type Exclude<T, U> = T extends U ? never : T;
Real-World Examples
Remember
never = impossible to have a value. Use it for always-throws functions and exhaustive switch defaults. The assertNever pattern ensures you handle every case of a union โ add a new case later and TypeScript catches the forgotten branch.
Quick Quiz
never means?