The Never Type

Never Type
πŸ‘¨β€πŸ’Ό Some functions never return. Not because they return void (nothing), but because they can't returnβ€”they throw an error or run forever.
function fail(message: string): never {
	throw new Error(message)
}
TypeScript uses this to mark code paths that can't continue. A never function is one that always throws or exits early.
🐨 Open
index.ts
and create these exported functions:
  1. throwError(message: string): never β€” always throws an error using the given message
  2. parseNumber(value: string): number β€” turn a string into a number; if the value cannot be parsed, call throwError with the exact message Invalid number; otherwise return the number
  3. ensurePositive(value: number): number β€” return the number when it is zero or positive; if it is negative, call throwError with the exact message Number must be positive

Required exports

throwError, parseNumber, ensurePositive

Completion criteria

  • throwError('Test error') throws with message Test error
  • parseNumber('42') returns 42
  • parseNumber('not-a-number') throws with message Invalid number
  • ensurePositive(5) returns 5
  • ensurePositive(0) returns 0 (zero is allowed)
  • ensurePositive(-1) throws with message Number must be positive
The key learning here is that parseNumber and ensurePositive should use the throwError function rather than throwing directly. This demonstrates how TypeScript's flow analysis understands that code after a never-returning function call is unreachable.

Please set the playground first

Loading "The Never Type"
Loading "The Never Type"