TypeScript Never Type

Introduction

TypeScript supports bottom types[fn:bottomtypes]. Cases where this occurs:

  • A function never returns (e.g. if the function body has src_js{while(true){}})
  • A function always throws (src_js{function foo(){throw new Error(‘Not Implemented’)}})

Syntax

let foo: never; // Okay
let foo: never = 123; // Error: Type number is not assignable to never
 
// Okay as the function's return type is `never`
let bar: never = (() => { throw new Error(`Throw my hands in the air like I just don't care`) })();

Footnotes

[fn:bottomtypes]https://en.wikipedia.org/wiki/Bottom\_type