What's the difference between any, unknown and never?
Take a moment.
Say your answer out loud or write it down first — retrieval is what makes it stick. Reveal when you're ready.
Short answer
any disables type checking entirely; unknown is a safe top type that requires narrowing before use; never is the bottom type — a value that can never occur, used for impossible states and exhaustive checks.
Detailed explanation
any opts out of TypeScript: everything is assignable to it and it propagates silently through your code, defeating the compiler. It exists for gradual migration and legacy interop. unknown is the type-safe counterpart — any value fits into unknown, but you can't *use* an unknown without narrowing first (typeof checks, instanceof, or assertion), so errors surface at compile time instead of runtime. Prefer unknown for external input: parsed JSON, event payloads, API responses. never occupies the opposite end: no value has that type. Functions that always throw or infinite-loop return never. Its killer feature is exhaustiveness: in a switch over a union, a default case assigning the value to never fails to compile if any variant wasn't handled — the compiler proves your handling is complete.
Example
function parse(input: string): unknown {
return JSON.parse(input); // don't trust it
}
const data = parse(raw);
// data.name ❌ error — narrow first:
if (typeof data === 'object' && data !== null && 'name' in data) {
console.log((data as { name: string }).name);
}
// Exhaustiveness with never:
type Shape =
| { kind: 'circle'; r: number }
| { kind: 'square'; s: number };
function area(shape: Shape): number {
switch (shape.kind) {
case 'circle': return Math.PI * shape.r ** 2;
case 'square': return shape.s ** 2;
default: {
const _exhaustive: never = shape; // compile error if a kind is missed
throw new Error(`Unhandled: ${_exhaustive}`);
}
}
}Interview tip
Frame them on an axis — any abandons safety, unknown defers trust, never proves impossibility. The exhaustive-switch trick is the detail interviewers wait for.
Common mistakes
- Reaching for any when unknown works fine.
- Saying never means undefined or null.