What is the difference between `any`, `unknown` and `never` in TypeScript?
Quiz · TypeScript
Easy
6 min
typescript
type-system
TL;DR
anyturns off type checking. Anything is assignable to it, and it's assignable to anything. It's an escape hatch that spreads unsafety.unknownis the type-safe top type. Anything is assignable to it, but you can't use it until you narrow it (withtypeof,instanceof, type guards or validation).neveris the bottom type, the empty set. No value has it. It's the return type of functions that never return (throw or loop forever), the result of impossible narrowing, and the tool for exhaustiveness checks.
any vs unknown
function parseA(json: string): any { return JSON.parse(json); }
function parseU(json: string): unknown { return JSON.parse(json); }
const a = parseA('{"x":1}');
a.foo.bar(); // compiles, crashes at runtime
const n: number = a; // compiles: any leaks into typed code
const u = parseU('{"x":1}');
u.x; // error: 'u' is of type 'unknown'
if (typeof u === 'object' && u !== null && 'x' in u && typeof u.x === 'number') {
u.x.toFixed(); // OK after narrowing
}
Rule of thumb: use unknown for values whose type you don't know yet (JSON, catch variables, external input, generic containers), and narrow or validate them, ideally with a schema library such as Zod or Valibot. Use any only in narrow, commented spots (migrations, typing gaps in libraries), and enable noImplicitAny (part of strict) so it never appears silently.
catch (e) variables are unknown under strict (via useUnknownInCatchVariables, TS 4.4+), so you must narrow them: if (e instanceof Error) e.message.
never
function fail(message: string): never {
throw new Error(message);
}
type Shape = { kind: 'circle'; r: number } | { kind: 'square'; size: number };
function area(s: Shape): number {
switch (s.kind) {
case 'circle': return Math.PI * s.r ** 2;
case 'square': return s.size ** 2;
default: {
const unreachable: never = s; // error here if a new kind is added and not handled
return unreachable;
}
}
}
Other places never shows up:
- Union filtering:
neverdisappears from unions (string | neverisstring), which is howExclude<T, U>works:T extends U ? never : T. - Impossible intersections:
string & numberisnever. Objects with conflicting discriminants reduce tonever. - Empty arrays inferred as
never[]in some contexts.
Type-theory view
| Type | Set of values | Assignable from | Assignable to |
|---|---|---|---|
unknown | All values | Everything | Only unknown and any |
any | Opt-out | Everything | Everything (unsound) |
never | Empty | Only never | Everything |
Pitfalls
as anycasts hide real bugs. Preferas unknown as Tonly when you truly must, or better, validate.Function,Objectand{}aren't safe "any object" types.{}means "any non-nullish value".- A function inferred to return
neverunexpectedly often signals a bug (an infinite loop, or every path throws). - ESLint's
@typescript-eslint/no-explicit-anyandno-unsafe-*rules help keepanyout.
In an interview, mention…
anydisables checking and is contagious.unknownforces narrowing.neveris the empty type.- Use
unknownfor external input with runtime validation, andcatchvariables areunknownunder strict. neverfor exhaustiveness checks and in conditional and utility types.- Lint and strict settings to limit
any.
References
Spotted a mistake or a rights concern? Report an issue