Day 52 of 100: Exhaustive Checking with never — Unions That Cannot Drift
Day 52 of 100 — 100 Days of Playwright with TypeScript Challenge. Phase: TypeScript. JavaScript (Days 1-33) → TypeScript (Days 34-63) → Playwright (Days 64-96) → AI Framework Capstone (Days 97-100).
🎭 Want to master this with real projects? Join the Playwright Automation Mastery course at The Testing Academy.
Unions grow. Switches silently miss new cases. The never trick makes the compiler refuse to let that happen.
Contents
The Exhaustiveness Pattern
type Status = 'passed' | 'failed' | 'skipped';
function color(s: Status): string {
switch (s) {
case 'passed': return 'green';
case 'failed': return 'red';
case 'skipped': return 'gray';
default: {
const exhausted: never = s; // s must be never here
throw new Error(`Unhandled status: ${exhausted}`);
}
}
}
// Later someone adds 'flaky' to Status:
// → const exhausted: never = s ERRORS at compile time
// → every switch missing 'flaky' is listed. Nothing drifts.
🚀 Level Up Your Playwright
From locators to CI pipelines — build a production-grade Playwright + TypeScript framework step by step.
Assert Functions
function assertDefined(v: T | null | undefined, label: string): asserts v is T {
if (v == null) throw new Error(`${label} was ${v}`);
}
const row: string | undefined = ['a'].find(x => x === 'a');
assertDefined(row, 'row');
row.toUpperCase(); // narrowed to string after the assert
QA Payoff
- Report formatters, status mappers, env routers — all switch over unions
- Union grows → compiler hands you the exact to-do list
- asserts functions give reusable narrowing with loud failures
Tomorrow (Day 53): Typed async patterns — Promise
🎓 Master Playwright End to End
Join hundreds of SDETs building real automation frameworks. Lifetime access, hands-on projects, and a job-ready portfolio.
