Docs
/
TypeScript
Chapter 20

20 — Advanced Patterns

Core Concepts

  • Branded/opaque types — prevent mixing structurally identical types
  • Builder pattern — type-safe fluent API construction
  • Type-safe event emitter — events with typed payloads
  • Phantom types — type params that exist only at compile time
  • Variance — covariance, contravariance, invariance
  • Recursive types — self-referencing type definitions
  • HKT simulation — higher-kinded types in TypeScript

Why These Patterns

  • Branded types prevent userId from being used as orderId
  • Builder pattern ensures required fields are set before building
  • Phantom types add compile-time safety without runtime cost
  • These patterns push TypeScript's type system to its limits