LLearnCode
Academy/typescript/Production TypeScript

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.

Loading progressEvidence grows with practice
Useful preparation · 1 prerequisite

Use this as a map, not a lock. If these ideas are familiar, continue.

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.

  1. Represent mutually exclusive states with a discriminated union.
  2. Attach only the data required by each state.
  3. Keep transition logic pure where possible.
  4. Types rule out some impossible shapes; tests and runtime authority must still enforce legal transitions.
Read the codetypescript · educational example
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;
}
  1. Start

    A new request becomes the current sending state.

  2. Match

    Only the current request may commit its result.

  3. 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 →