profileRyan KesPGP keyI build stuffEmailGithubTwitterLast.fmMastodonMatrix

TypeScript Never Type

Introduction

TypeScript supports bottom types1. Cases where this occurs:

  • A function never returns (e.g. if the function body has while(true){})
  • A function always throws (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