Explain TypeScript's any, unknown, void and never. Why is unknown safer than any?
Show the explanation
Model answer.
- `any` switches type checking off for that value: you can call anything on it and assign it anywhere. Errors slip through silently.
- `unknown` means "could be anything, so prove it before use". You must narrow it (with
typeof,instanceof, or a type guard) before doing anything type-specific. - `void` is the return type of a function that returns nothing useful.
- `never` is the type of values that never occur: a function that always throws or loops forever, or the leftover case after exhaustive checks.
function parse(json: string): unknown { return JSON.parse(json); }
const data = parse('{"n":1}');
// data.n; // error: object is of type unknown
if (typeof data === 'object' && data !== null && 'n' in data) { /* narrowed */ }
type Shape = { kind: 'circle'; r: number } | { kind: 'square'; s: number };
function area(x: Shape): number {
switch (x.kind) {
case 'circle': return Math.PI * x.r ** 2;
case 'square': return x.s ** 2;
default: const unreachable: never = x; return unreachable; // compile error if a case is missed
}
}Use unknown for untrusted input (API responses, JSON.parse, catch variables), and keep any as a last resort.
Original UniCircle question. Last reviewed 22 Oct 2026.
