GAP·MAP
← all breakdowns
TypeScriptType SafetyType Narrowingjunior level

Any vs Unknown in TypeScript

the question

What is the difference between `any` and `unknown` in TypeScript, and when would you use each one?

what they're testing: The interviewer is checking whether you understand where TypeScript stops enforcing safety and how to preserve it around values whose type is not yet known.

a strong answer

Both any and unknown can hold values of any type. The difference appears when you use the value. any tells TypeScript to trust the code: property access, calls, and assignment to a more specific type are allowed without proof. With unknown, those operations are rejected until you narrow or assert the value to a suitable type.

That makes unknown the better fit at boundaries where data really is not known yet, such as catch variables or unvalidated input. Narrow it with typeof, instanceof, or a custom type guard, then TypeScript allows the operations valid for the narrowed type. For example, check typeof value === "string" before calling value.toUpperCase().

A type assertion can also turn unknown into a specific type, but it adds no runtime check; a wrong assertion merely hides the risk. Reach for any only when deliberately opting out of checking, often as a temporary bridge for untyped or hard-to-model code. The useful shorthand is: any says "skip the proof," while unknown says "this could be anything, so prove what it is before using it."

Where people slip

the tempting wrong answer, and what's actually true

  • `unknown` is just another name for `any`.

    Both accept values of any type, but `unknown` blocks unchecked operations and assignments that `any` permits.

  • You cannot assign any value to a variable typed `unknown`.

    Any value is assignable to `unknown`; the restriction applies when you try to use or assign that value as a more specific type.

  • A type assertion on `unknown` validates the value at runtime.

    Type assertions are removed during compilation and perform no runtime validation.

  • `noImplicitAny` prevents every use of `any` in a project.

    `noImplicitAny` reports implicit `any` inference, but it does not forbid explicit `any` annotations.

If they push further

How do you safely use a value typed `unknown`?

Narrow it with runtime evidence such as `typeof`, `instanceof`, or a type guard, then use only the operations supported by the narrowed type.

Why are catch variables often typed `unknown`?

JavaScript can throw any value, so `unknown` requires a check before treating the caught value as an `Error` or another type with a string `message`.

When is `any` still reasonable?

It can be a deliberate, localized escape hatch for migration or untyped code, but the loss of checking should be contained and temporary where possible.

Sources

Now answer it yourself.

Reading a strong answer is easy. Producing one under pressure is the skill the interview tests. Gapmap grades your answer against the same bar an interviewer would.

was this useful?

beta

The interviewer part is in the works.

The diagnostic, personal maps, and AI mock interviews are being finished right now. The notes stay free either way. Leave an email and you'll get the first-cohort invite, plus a month of Pro when it opens.