On this page
  1. never
  2. Syntax & Overview
  3. Notes & Pitfalls
  4. Example

never

Prev: `unknown` | Up: builtins | Next: `void`
Rel: builtins, Structural Typing & Narrowing
Ref: TypeScript Docs — never
Tags: #public #web #typescript #builtins #tier/core


Syntax & Overview

function fail(msg: string): never { throw new Error(msg); }

Represents values that can never occur (functions that always throw/loop forever, or impossible branches after exhaustive narrowing).

Notes & Pitfalls

Example

type Shape = { kind: "circle"; r: number } | { kind: "square"; s: number };

function area(shape: Shape): number {
  switch (shape.kind) {
    case "circle": return Math.PI * shape.r ** 2;
    case "square": return shape.s ** 2;
    default: {
      const _exhaustive: never = shape;
      return _exhaustive;
    }
  }
}

Linked from

Related notes