๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท
๐Ÿ“˜ TypeScript

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 โ€ฆ

8 min 10 XP Lesson 17 of 21
never type
๐ŸŒ

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 | never simplifies to string โ€” 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

โ€ข <code>function exhaustiveCheck(x: never): never { throw new Error('Missed case: ' + JSON.stringify(x)); }</code> โ€ข Error handler: <code>function panic(msg: string): never { throw new Error(msg); }</code> โ€ข In discriminated union switch โ€” default: assertNever(action) โ€ข Conditional type bottoms: <code>T extends string ? T : never</code> โ€” filters to only string subtypes

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

1 / 2

never means?