Skip to the page

How TypeScript thinks · core

any vs unknown vs never

unknown is the safe 'could be anything' type you must narrow before use, any switches checking off, and never is the type with no values, used for impossible cases and exhaustiveness checks.

The notes

What you must know

  • any disables type checking and spreads silently to everything it touches. Avoid it; noImplicitAny (part of strict) stops accidental any.
  • unknown accepts any value but you can't use it until you narrow it (typeof, instanceof, a type guard, or validation). Use it for JSON, catch variables and untrusted input.
  • never has no values. A function that always throws returns never. In a switch over a union, assigning the leftover to never makes the compiler fail when someone adds a new case.
  • In catch (e), e is unknown under strict (useUnknownInCatchVariables), so check e instanceof Error before reading e.message.
  • void means 'the return value is not used', not 'no value'; undefined is a real value.

The red pen

Common traps: mistake → fix

  • Mistake: "unknown is the same as any"

    Fix: any lets you do anything; unknown lets you do nothing until you narrow it.

  • Mistake: "never is for functions that return nothing"

    Fix: that is void; never means the function never returns at all.

Easy to hard

Questions interviewers ask, easy to hard

  1. Difference between any and unknown?
  2. Where do you use never in real code?
  3. How do you write an exhaustive switch that breaks the build when a new variant is added?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview