<>
23
TypeScript design patterns

TypeScript design patterns

Use the type system as a design tool. Eight lessons on discriminated unions, branded types, and patterns that prevent bugs at compile time.

~ 125 min·8 lessons·updated apr 2026

What you'll learn


Lessons

Read in order. Each lesson stands on its own, but the order is deliberate.

8 lessons · ~120 min total

  1. 01
    Make illegal states unrepresentable
    The classic loading flag bug. Why a discriminated union beats a bag of optional fields.
    16 min
  2. 02
    Branded and opaque types
    Stop mixing UserId and OrderId. A zero-runtime trick that makes the compiler help.
    14 min
  3. 03
    Readonly everywhere
    readonly properties, readonly arrays, as const. Where shallow ends and deep begins.
    14 min
  4. 04
    Exhaustiveness with never
    assertNever in a switch default. Add a new case and the compiler tells you what to fix.
    15 min
  5. 05
    Builder patterns and method chaining
    Fluent APIs that refuse to compile until you call the required steps.
    17 min
  6. 06
    Result types
    Return failure instead of throwing it. When this beats try/catch, and when it doesn't.
    14 min
  7. 07
    Tagged unions for events and actions
    Reducer actions, event handlers, command objects. One pattern that scales.
    16 min
  8. 08
    The unknown problem
    unknown vs any. Why one keeps you safe and the other quietly lies.
    14 min

Prerequisites

  • · typescript-essentials
  • · Comfortable reading union types

After this, try

  • Advanced types
  • Schema-driven design
  • Library type design