0Pricing
TypeScript Academy · Lesson

Exhaustiveness with never (intro)

Use never to enforce exhaustive switches on unions and catch missing cases early.

Intro

Goal: Ensure your switch handles all members of a union. The never type helps the compiler alert you when a new case is missing.

Union setup

Create a discriminated union with a literal tag (e.g., kind) to switch on later.

type Circle = { kind: "circle"; radius: number };
type Square = { kind: "square"; size: number };

type Shape = Circle | Square;

All lessons in this course

  1. typeof, equality, truthiness narrowing
  2. in, instanceof, discriminated unions
  3. Exhaustiveness with never (intro)
← Back to TypeScript Academy