Skip to main content

Any Type

What any does

any switches off type checking for a value. It can hold anything and be assigned to anything — TypeScript stops guarding it entirely.

let notSure: any = 4;
notSure = "now a string"; // Valid: allowed
notSure = true; // Valid: allowed
notSure.foo.bar.baz(); // Valid: no error — even though this will crash at runtime

Use with caution — any is contagious

Every any is a hole in your type safety, and it spreads: values derived from an any become any too, silently disabling checks far from where you opted out.

const data: any = getData();
const name = data.name; // name is also 'any' — no checking downstream
name.toFixed(); // no error, crashes if name is a string

Using any too much throws away the entire reason you added TypeScript.

Reach for unknown instead

When you truly don't know a type yet (an API response, user input), prefer unknown. It's the safe counterpart: it accepts anything, but forces you to check before you use it.

let value: unknown = getData();
value.toUpperCase(); // Error — must narrow first
if (typeof value === "string") {
value.toUpperCase(); // Valid: now allowed
}

any says "trust me, stop checking." unknown says "I don't know yet, make me prove it." Default to unknown; treat any as a last resort.