Type Hierarchy
What
Types in TypeScript exist in a hierarchy. At the top are the “widest” types (any and unknown), and at the bottom is the “narrowest” type (never).
How
Assignability flows “up” the hierarchy. You can assign a specific type to a broader one, but not the other way around without narrowing:
- Widest:
any,unknown - General:
string,number,boolean - Specific:
"hello",42,true - Narrowest:
never
Why
Understanding the hierarchy helps you grasp why narrowing is necessary. When you narrow a type, you are moving “down” the hierarchy to a more specific type where more properties and methods are guaranteed to exist.
Summary
Narrowing is the act of moving from wide, uncertain types at the top of the hierarchy to narrow, certain types at the bottom.