- All Courses
- TypeScript Deep Dive
- The Type System Underneath
- unknown, never, and the any escape hatch
unknown, never, and the any escape hatch
any switches the checker off for that value and everything it touches.
Overview
The top type, the bottom type, and the one that gives up
any switches the checker off for that value and everything it touches. It is contagious: a single any at an API boundary can silently untype an entire module downstream.
unknown is the safe alternative. Anything is assignable to it, but you can do nothing with it until you narrow it, which forces the validation to happen exactly where the untrusted data enters.
never is the type with no values. Seeing it usually means the compiler has proved a branch is unreachable — which is useful for exhaustiveness checks and alarming when it appears somewhere you expected real data.
In this lesson you will:
- Use unknown for values you have not validated
- Recognise what never means when it appears
- Contain any instead of letting it spread
Resources
Notes are not saved yet — they clear when you leave this page.
Literal types and const assertions
6m
Writing your first generic
13m