any vs unknown in TypeScript
Compare any and unknown in TypeScript 6: how each behaves, why unknown is the safe top type, how to narrow it, and the few places where any is still needed.

Both any and unknown accept every value. You can assign a string, a number, an object or undefined to either. The difference is what the compiler lets you do afterward. With any, everything: call it, index it, read any property, pass it anywhere. With unknown, nothing, until you prove what it is. That one difference decides whether the type system helps you or steps aside. This post compares any and unknown in TypeScript 6.x: how each behaves on assignment and use, how to narrow unknown, how any spreads, which compiler options and lint rules keep it in check, and the rare cases where any is still the right choice. The comparison is identical in the native TypeScript 7 compiler. The reference is the everyday types chapter of the handbook and the unknown type section.
Side by side
| Behavior | any | unknown |
|---|---|---|
| Accepts any value on assignment | Yes | Yes |
| Assignable to other types | Yes, to everything | Only to unknown and any |
| Property access and calls | Allowed, unchecked | Error until narrowed |
Narrowing with typeof, instanceof, guards | Not needed | Required |
| Spreads into inferred types | Yes, infects results | No |
| Role in the type system | Turns checking off | Top type: the set of all values |
unknown is the type-safe counterpart of any. It says "this could be anything, so check before use". any says "stop checking".
Using each
Here is the same function written both ways.
function shoutAny(value: any): string {
return value.toUpperCase();
}
console.log(shoutAny("hello"));
console.log(shoutAny(42));HELLO
TypeError: value.toUpperCase is not a functionBoth calls compile. The second fails at run time, because a number has no toUpperCase. The compiler did not help, since any told it not to.
function shoutUnknown(value: unknown): string {
return value.toUpperCase();
}unknown-use.ts(2,16): error TS18046: 'value' is of type 'unknown'.The error is the point. To fix it, narrow the value to a type that has the method.
function shoutUnknown(value: unknown): string {
if (typeof value === "string") {
return value.toUpperCase();
}
return String(value);
}
console.log(shoutUnknown("hello"));
console.log(shoutUnknown(42));HELLO
42Inside the if, value is a string. The function now handles every input and cannot throw on the wrong type. The cost was three lines, and each one is a decision that the any version skipped.
Narrowing unknown
unknown narrows with the same tools as any union: typeof for primitives, instanceof for classes, in for properties, equality checks, and user defined type guards.
function describe(value: unknown): string {
if (typeof value === "number") return `number ${value.toFixed(1)}`;
if (value instanceof Date) return `date ${value.getUTCFullYear()}`;
if (Array.isArray(value)) return `array of ${value.length}`;
if (typeof value === "object" && value !== null && "id" in value) {
return `object with id ${String(value.id)}`;
}
return "something else";
}
console.log(describe(3));
console.log(describe(new Date(0)));
console.log(describe([1, 2, 3]));
console.log(describe({ id: 7 }));
console.log(describe(null));number 3.0
date 1970
array of 3
object with id 7
something elseArray.isArray narrows an unknown to any[], which is one place where any appears from the standard library. If you want a stricter element type, check the elements too, or assert after a check you wrote.
For shapes you will reuse, write a type guard that returns value is T. That puts the check in one function and gives every caller the narrowed type.
How any spreads
The worst property of any is that it is contagious. A value of type any flows into everything it touches, and the result is often any too.
const data: any = JSON.parse('{"count": 3}');
const doubled = data.count * 2;
const label = `Total: ${data.count}`;
const copy = { ...data, extra: true };
doubled.toUpperCase();spread.ts(7,9): error TS2339: Property 'toUpperCase' does not exist on type 'number'.The types of doubled, label and copy are number, string and any, because arithmetic and templates produce known types, but the spread of an any object gives any. The last line is an error only because doubled is a number. The example shows both sides: some operations turn any back into a typed value, and others pass it along. The effect on a large codebase is that one any at an entry point, such as JSON.parse or response.json(), can make dozens of downstream values unchecked without anyone noticing.
unknown does not spread. Anything computed from an unknown needs a check first, so the uncertainty stays at the boundary, where you handle it once.
any in assignments
A value of type any is assignable to every type, which is the hole through which bad data enters typed code.
const loose: any = "text";
const count: number = loose;
console.log(count.toFixed(2));TypeError: count.toFixed is not a functionThe assignment compiles, and the variable declared as number holds a string. With unknown, the same assignment is an error, because unknown is assignable only to unknown and any.
const loose: unknown = "text";
const count: number = loose;assign-unknown.ts(2,7): error TS2322: Type 'unknown' is not assignable to type 'number'.Where to use unknown
Use unknown for values whose type you do not control. The result of JSON.parse, a caught error, a message from another window, a request body, the payload of an event, data read from storage. In each case, accept unknown, validate, and pass on a typed value.
try {
throw "a string, not an Error";
} catch (error: unknown) {
const message = error instanceof Error ? error.message : String(error);
console.log(message);
}a string, not an ErrorUse unknown for function parameters that accept anything and only pass the value along, such as a logger: function log(value: unknown): void. The caller can pass anything, and the function cannot misuse it.
Where any is still reasonable
There are narrow cases. A generic constraint that must accept any function, for example (...args: any[]) => any, uses any because unknown in a parameter position is too strict: a function that takes a string is not assignable to a function that takes unknown. Some tools of this kind, such as a ReturnType helper or a wrapper that preserves signatures, need the any constraint.
type Fn = (...args: any[]) => unknown;
function logged<F extends Fn>(fn: F): F {
return ((...args: Parameters<F>) => {
console.log("call", args.length);
return fn(...args);
}) as F;
}
const add = logged((a: number, b: number) => a + b);
console.log(add(1, 2));call 2
3The any[] lets any function match the constraint, and the result keeps its own precise type through the generic. The any stays inside the helper, and callers see typed code.
Other legitimate uses are migrations from JavaScript, where an explicit any marks code not yet typed, and interop with a library that has no types and where a declaration is not worth writing yet. In both cases, prefer unknown if you can, and mark the any with a comment.
Keeping any out
Several tools help. The compiler option noImplicitAny, part of strict, reports places where the compiler would silently infer any. The typescript-eslint rule no-explicit-any reports the word itself, and the family of no-unsafe-* rules reports operations on values of type any, which finds the places where it spreads. Setting these in CI makes any a visible, deliberate choice.
In the standard library, a few signatures return any: JSON.parse, Response.json and Array.isArray's narrowed result. Wrap the first two in helpers that return unknown, so the project code never sees the any.
A quick rule for reviews
When you see any in a review, ask one question: what is the real type of this value? If someone can answer, write that type. If nobody can, the value comes from outside, and the answer is unknown plus a check. If the answer is "it is too hard to express", look for a generic or an overload before accepting the any, since most such cases have a typed solution. A habit of asking that single question removes nearly every unnecessary any over time, and the few that remain will each have a reason written next to them, which is how a healthy codebase handles an escape hatch.


