Q&A-3Walk me through `any`, `unknown` and `never`: how each one is assignable, and where you'd actually reach for each in a React codebase.
Answer
The 90-second answer. These three are the top, the bottom, and the escape hatch in TypeScript's type lattice.
unknownis the top type. Every type is assignable to it; it is assignable to nothing exceptanyand itself. You can store, pass and compare anunknown, but you cannot read a property, call it, or add it to a number until you narrow it.neveris the bottom type. It is assignable to everything; nothing is assignable to it exceptneveritself. It describes a value that cannot exist.anyis assignable in both directions with every type: with one exception,never. Every operation on it type-checks. That is not a position in the lattice; it is the switch that turns the checker off for that value and everything derived from it.
| from ↓ to → | any | unknown | never | string |
|---|---|---|---|---|
any | ✓ | ✓ | ✗ | ✓ |
unknown | ✓ | ✓ | ✗ | ✗ |
never | ✓ | ✓ | ✓ | ✓ |
string | ✓ | ✓ | ✗ | ✓ |
The any → never cell is the one asymmetry worth memorising: any is assignable to every type except never, so const x: never = someAny is an error. Union and intersection behaviour falls out of the same positions: unknown | string collapses to unknown, never | string to string, unknown & string to string, never & string to never.
Where each is correct. unknown at every boundary you don't control: JSON.parse, response.json(), postMessage, catch bindings, third-party callbacks. never for exhaustiveness and for filtering unions. any almost nowhere: a short-lived bridge during a JS→TS migration, and even there // @ts-expect-error with a reason beats it, because it starts failing once the underlying error is fixed.
The boundary is the argument that matters. lib.dom types Response.json() as Promise<any>, so the leak starts at the first await:
// any: compiles, ships, throws in production when the API renames a field
const data = await res.json();
return data.user.email; // no error even when user is null
// unknown: the annotation is the whole fix
const data: unknown = await res.json();
return data.user.email; // TS18046: 'data' is of type 'unknown'
Now the compiler forces a decision. Either a hand-written guard:
function isUser(v: unknown): v is User {
if (typeof v !== 'object' || v === null) return false;
const o = v as Record<string, unknown>;
return typeof o.id === 'string' && typeof o.email === 'string';
}
or a schema validator at the edge (zod, valibot) whose parse returns a typed value or throws with a path like user.email: Expected string, received null. Either way the failure moves from a component render to the network boundary, where you can log it, render an error state, and keep the rest of the app typed.
never earns its place in the exhaustiveness check:
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: {
const exhaustive: never = s; // a third variant breaks the build here
return exhaustive;
}
}
}
Failure modes. any is contagious: one Promise<any> infects every call site downstream and a field rename ships green. Detect it with @typescript-eslint/no-unsafe-assignment, no-unsafe-member-access and no-unsafe-return in CI: they fire on any flowing into typed code, which no-explicit-any alone does not catch. catch (e: any) reintroduces the same bug for thrown non-Errors: throw 'boom' leaves e.message undefined. Since TS 4.4 useUnknownInCatchVariables (on under strict) types the binding unknown, and the fix is e instanceof Error ? e.message : String(e). A never return annotation is trusted rather than verified at the call site: callers use it for control-flow analysis, so a function annotated : never that can actually return makes the compiler treat everything after the call as unreachable and silently drop narrowing. The symptom is a missing error, not a type error. The guard is that every path in the body throws or loops, which is a review and test obligation.
Curated: · Written: · Reviewed: