any, unknown, never
TypeScript · Reference cheat sheet
any, unknown, never
TypeScript · Reference cheat sheet
📋 Overview
any disables checking; unknown is the safe top type (must narrow before use); never is the bottom type (no values). Prefer unknown over any at boundaries; use never for exhaustiveness and impossible states.
🔧 Core concepts
| Type | Assignability | Use |
|---|---|---|
any | Assignable to/from almost everything | Escape hatch, gradual typing |
unknown | Accepts any value; not assignable out without narrowing | Untrusted input, JSON, catch |
never | Assignable to every type; nothing assignable to it | Exhaustive checks, empty unions |
strict/noImplicitAny— missing annotations become errors, not implicitany.- Empty intersection —
string & number→never. - Functions that throw / infinite loop — return type
never.
💡 Examples
function parseJson(raw: string): unknown {
return JSON.parse(raw);
}
const data = parseJson('{"n":1}');
if (
typeof data === "object" &&
data !== null &&
"n" in data &&
typeof (data as { n: unknown }).n === "number"
) {
console.log((data as { n: number }).n);
}
function assertNever(x: never): never {
throw new Error(`Unexpected: ${x}`);
}
type Shape = { kind: "circle"; r: number } | { kind: "square"; s: number };
function area(s: Shape): number {
switch (s.kind) {
case "circle":
return Math.PI * s.r ** 2;
case "square":
return s.s ** 2;
default:
return assertNever(s); // error if a variant is missing
}
}
function fail(msg: string): never {
throw new Error(msg);
}
// any: opt-out (avoid in new code)
function legacy(x: any) {
return x.foo.bar; // no check
}⚠️ Pitfalls
anyinfects call sites — oneanycan silence whole chains.catch (e)isunknownunderuseUnknownInCatchVariables(recommended).never[]is assignable to any array type — don’t confuse with “empty array of T”.- Returning
neverfrom a branch that can complete is a type error. - Prefer typed Zod / validators over casting
unknownto concrete types blindly.