Exhaustive error handling with discriminated unions
Write exhaustive handlers for discriminated unions using switch+never, helper utilities, and compile-time coverage checks across modules.
Intro
Goal: Guarantee every union member is handled. You will use switch + never, a tiny exhaustiveCheck utility, and see how to keep coverage across files.
- Discriminants (tag fields)
- Exhaustive switches
- Fail-fast defaults
Discriminated union
A discriminated union has a shared tag (here kind). Each variant carries its own data.
export type Payment =
| { kind: "Card"; last4: string }
| { kind: "Cash" }
| { kind: "Wire"; iban: string }All lessons in this course
- Result/Either style types
- Exhaustive error handling with discriminated unions