Docs
/
TypeScript
Chapter 19

19 — Error Handling Patterns

Core Concepts

  • Result/Either type — represent success or failure without exceptions
  • Custom error classes — typed error hierarchies with instanceof checking
  • Type-safe try/catch — catch block typing and error narrowing
  • Error hierarchies — base error class with specific subtypes
  • Exhaustive error handling — discriminated union for error types
  • Async error patterns — typed Promise rejection, error boundaries

Why Typed Errors Matter

  • JavaScript catch gives you unknown — no type info
  • Thrown errors can be anything (string, number, Error, etc.)
  • Result types make error handling explicit in the type system
  • Discriminated error unions ensure you handle every case