A shared literal field (kind, type, status) lets switch narrow safely—ideal for API results and UI state machines.
Modeling API results
type ApiResult<T> =
| { ok: true; data: T }
| { ok: false; status: number; error: string };
Consumers switch on ok—TypeScript knows which fields exist in each branch.
Self-check
- Add a third variant
loading—what breaks if you forget to update switches?
Challenge
Shape area
- Model
Circle | Rectanglewith akindfield. - Write
area(shape)usingswitchonkind. - Print areas for one of each.
Done when: two numeric areas in terminal.