advanced · 25 minute study
Design away impossible states
Several independent booleans can describe contradictory states. A small explicit state machine makes transitions reviewable and testable.
Useful preparation · 1 prerequisite
Use this as a map, not a lock. If these ideas are familiar, continue.
- Make failure part of the API contract — Loading, unauthorized, invalid input and network failure should lead to deliberate behavior instead of undefined access and fake success.
First, build a mental model
A state is a complete description of one valid situation. A transition consumes the current state and an event and returns the next state or rejects an invalid move.
- Represent mutually exclusive states with a discriminated union.
- Attach only the data required by each state.
- Keep transition logic pure where possible.
- Types rule out some impossible shapes; tests and runtime authority must still enforce legal transitions.
type Upload = { kind: 'idle' } | { kind: 'sending'; requestId: string } | { kind: 'done'; assetId: string };
type Event = { type: 'start'; requestId: string } | { type: 'succeed'; requestId: string; assetId: string };
function update(state: Upload, event: Event): Upload {
if (event.type === 'start') return { kind: 'sending', requestId: event.requestId };
if (state.kind === 'sending' && state.requestId === event.requestId)
return { kind: 'done', assetId: event.assetId };
return state;
}- Start
A new request becomes the current sending state.
- Match
Only the current request may commit its result.
- Stale result
A result from an older request leaves state unchanged.
Production thinking
Take this beyond the example.
Use request identity and tenant/account scope in async transitions. Keep production mutations under server/database authority even when the UI model is precise.
Sources and further reading
Your evidence
unseen is a local learning signal, not a credential or proof of production competence.
Explain the mental model, then explicitly acknowledge it. Opening a page is not evidence.
Supported retries can complete this lesson. Independent evidence and delayed review determine the skill stage.
Next lesson · Type components around ownership →