Type Narrowing
Question
What is type narrowing?
Hint
Narrowing happens via type guards, conditionals, and control flow analysis.
Explanation
Type narrowing is the process of refining a type to a more specific type within a code block. TypeScript narrows types through: 1) typeof checks (if typeof x === "string" narrows to string). 2) instanceof checks. 3) in operator (if "prop" in obj). 4) Equality checks (if x === null). 5) Discriminated unions (if obj.kind === "circle"). 6) Custom type guards (x is string). Each narrowing is scoped to the conditional block. Narrowing enables safe property access on union types.
Related Resources
Related Lessons
- Type Narrowing
Learn narrowing.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.