Lesson 1.4

unknown, never, and the any escape hatch

any switches the checker off for that value and everything it touches.

8mIntermediate21.6k students

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

Previous Lesson
Next Lesson
unknown, never, and the any escape hatch — TypeScript Deep Dive — Vertex