TypeScript narrowing and discriminated unions: safer control flow

11 steps · about 3 h 15 min · Intermediate

  1. TypeScript: Documentation - Narrowing (doc, typescriptlang.org, 30 min)

    The official handbook chapter covers typeof, equality, in, and discriminated unions in one place, so it comes first.

  2. Narrowing (article, TypeScript New Handbook, 20 min)

    A second walkthrough of the same ideas, with a step-by-step example of why an optional-field Shape fails to narrow.

  3. Unions, Literals, and Narrowing (article, Total TypeScript, 35 min)

    Adds exercises on unions, literals, never and switch-based narrowing so you practise what the docs explain.

  4. TypeScript Type Guards & Narrowing: Discriminated Unions, Predicates & typeof (article, rishikc.com, 20 min)

    Shows realistic Result-type examples and custom type predicates after you know the basics.

  5. Runnable TypeScript Docs: Discriminated Unions (doc, Coddy, 15 min)

    Runnable examples cover tag rules, plus the common reason narrowing fails (a member typed kind: string).

  6. Runnable TypeScript Docs: never Type (doc, Coddy, 10 min)

    Explains never as the type left when every case is ruled out, which is the idea behind exhaustiveness checks.

  7. Discriminated Unions + never: Exhaustive Checks at Compile Time (article, DEV Community (Gabriel Anhaia), 12 min)

    Teaches the assertNever helper and how to apply it consistently in a real codebase.

  8. Discriminated Unions (guide, TypeScript Deep Dive (Basarat), 10 min)

    Compares the exhaustive-check variants (const never, throw helper, strictNullChecks return) and a Redux example.

  9. TypeScript: Documentation - TypeScript 4.6 (article, typescriptlang.org, 8 min)

    Shows that narrowing also works through destructured discriminants and dependent parameters.

  10. Discriminated Unions and Tagged Types in TypeScript (article, Josh Lehman, 15 min)

    Applies the pattern to application state and nested unions, making impossible states unrepresentable.

  11. TypeScript Advanced #5: Discriminated unions and type guards in depth (article, School of Web, 20 min)

    Finishes with user-defined type guards, assertion functions, branded types, and the Result pattern, plus warnings about unsafe guards.