🔷TypeScript Type Narrowing
Replace unsafe `as` casts with control-flow narrowing, discriminated unions, and type predicates — then build a type-safe API client that narrows responses by status code.
Phase 1Unions and Control Flow
Understand unions and control-flow narrowing basics
A union is a promise you haven't kept yet
6 minA union is a promise you haven't kept yet
The compiler reads your if-statements
6 minThe compiler reads your if-statements
typeof only gets you halfway
7 mintypeof only gets you halfway
The never type is your safety net
7 minThe never type is your safety net
Phase 2Replacing Casts with Guards
Replace casts with typeof, instanceof, and in checks
One shared property, zero ambiguity
7 minOne shared property, zero ambiguity
Teach the compiler what you already know
7 minTeach the compiler what you already know
Assert once, narrow everything after
7 minAssert once, narrow everything after
filter(Boolean) lies — here's what works
6 minfilter(Boolean) lies — here's what works
Your narrowed type forgets itself in callbacks
7 minYour narrowed type forgets itself in callbacks
Phase 3Discriminated Unions and Predicates
Compare discriminated unions, predicates, and assertions
Your API returns three shapes — pick the safe one
7 minYour API returns three shapes — pick the safe one
Your validation layer is a hidden type boundary
7 minYour validation layer is a hidden type boundary
Guard at the gate or check at every door
7 minGuard at the gate or check at every door
The callback ate your type — and the fix is boring
7 minThe callback ate your type — and the fix is boring
Phase 4Type-Safe API Client
Build a type-safe API client narrowing by status
Build an API client that narrows by status code
8 minBuild an API client that narrows by status code
Frequently asked questions
- What is a discriminated union in TypeScript?
- This is covered in the “TypeScript Type Narrowing” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- When should I write a type predicate?
- This is covered in the “TypeScript Type Narrowing” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- How does the never type help with exhaustiveness?
- This is covered in the “TypeScript Type Narrowing” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- Why does my narrowed type widen inside a callback?
- This is covered in the “TypeScript Type Narrowing” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
Related paths
🐍Python Decorators Introduction
Build one mental model for Python decorators that covers closures, argument passing, functools.wraps, and stacking — then ship a working caching or logging decorator from scratch in under 30 lines.
🦀Rust Lifetimes Explained
Stop reading `'a` as line noise and start reading it as scope arithmetic — one failing snippet at a time — until you can thread lifetimes through a small parser or iterator adapter without fighting the borrow checker.
☸️Kubernetes Core Concepts
Stop drowning in 30+ resource types. Build the mental model one primitive at a time -- pods, deployments, services, ingress, config -- then deploy a real app with rolling updates and health checks.
📈Big O Intuition
Stop treating Big O as math you memorized for an interview — build the intuition to spot O(n²) disasters, pick the right data structure without thinking, and rewrite a slow function from O(n²) to O(n) in under five minutes.