TypeScript Type Narrowing
Narrow types safely using type guards, assertions, and TypeScript's control flow analysis.
Usage
Share code with type narrowing needs and get the right approach for safe type refinement.
Examples
- "How do I narrow a union type in a switch statement?"
- "Write a custom type guard for my API response"
- "What's the difference between type guards and type assertions?"
Guidelines
- Show built-in narrowing: typeof, instanceof, in, equality
- Cover custom type guard functions with is return type
- Include assertion functions with asserts return type
- Explain control flow analysis and its limitations
- Warn against using type assertions (as) when type guards are safer