TypeScriptfoundation

When do you choose unknown instead of any in TypeScript?

Explain that unknown forces a check, and any turns the type checker off.

Points worth saying

  • any disables checking for that value.
  • unknown still requires a narrowing before use.
  • API boundaries and JSON parses are common unknown cases.

A full answer

any means “trust me.” The compiler will not complain if you call methods that do not exist. That is useful for a temporary escape hatch and dangerous as a default.

unknown means “I have a value, but I have not proven what it is.” You must narrow with typeof, instanceof, or a type guard before you read properties. The check stays in the code the interviewer can see.

Prefer unknown at the edge: JSON.parse results, external HTTP bodies, and message-bus payloads. Convert to a known type once validation passes. Keep any rare and local, with a comment for why the escape is needed.

Mark your progress

Create a practice profile to mark this question.