any vs unknown vs never (Interview-Ready)
How to choose any/unknown/never, why unknown is safer, and how never appears in narrowing and exhaustive checks.
F
Frontend Interview Team
February 08, 2026
30‑second interview answer
anydisables type safety (TypeScript stops helping).unknownis a safe top type: you must narrow before using it.neverrepresents an impossible value—often used for exhaustive checks and to model code paths that can’t happen.
any: the escape hatch
let x: any = 123;
x.toUpperCase(); // no error at compile time (but runtime crash)Use any only when:
- you’re integrating a truly untyped library
- you’re in migration mode
Even then, prefer unknown + narrowing.
unknown: safe top type
let value: unknown;
// value.toUpperCase(); // ❌ error: can't use unknown directly
if (typeof value === 'string') {
value.toUpperCase(); // ✅
}Pattern: parse/validate boundary
function parseJson(json: string): unknown {
return JSON.parse(json);
}
const data = parseJson('{"x":1}');
// validate / narrow before usingnever: impossible values
1) Exhaustive switch
type Status = 'idle' | 'loading' | 'success' | 'error';
function assertNever(x: never): never {
throw new Error('Unexpected value: ' + x);
}
function render(status: Status) {
switch (status) {
case 'idle':
case 'loading':
return '...';
case 'success':
return 'OK';
case 'error':
return 'Oops';
default:
return assertNever(status); // ✅ if a new Status is added, TS warns
}
}2) Narrowing removes possibilities
function fail(msg: string): never {
throw new Error(msg);
}Common interview traps
- “Why not just use any?”
- Because it removes correctness guarantees.
- “Is unknown the same as any?”
- No. Unknown forces narrowing.
- “Where does never show up in real code?”
- Exhaustive checks, reducers, discriminated unions.
Mini Q&A
Q1: What’s the difference between any and unknown?
- unknown is safe; you must narrow.
Q2: What is never used for?
- exhaustive checks and impossible paths.
Q3: When is any acceptable?
- temporary migration, truly untyped integration.
Summary checklist
- I avoid
anyby default. - I use
unknownat boundaries. - I use
neverfor exhaustive checks.