In modern TypeScript, there are at least a dozen ways to narrow types
In modern TypeScript, there are at least a dozen ways to narrow types.
1. typeof Type guards
2. Truthiness narrowing
3. Equality narrowing
4. `in` operator narrowing
5. `instanceof` narrowing
6. Control flow analysis
7. Type predicates / user-defined type guards
8. Discriminated unions
9. Assertion functions
10. Type assertions
11. Exhaustiveness checking via `never`
12. Exhaustiveness checking via `satisfies`
Quite a list! The trick is deciding when to use each.
I'm exploring how to choose between all these in my upcoming TS course - publishing March 2025.