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
- typeof, equality, truthiness narrowing
- in, instanceof, discriminated unions
- Exhaustiveness with never (intro)