Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Primitive and Special Types

The TypeScript any type

Reference for any in TypeScript: how it disables checking, implicit any, noImplicitAny, leaking any, better alternatives such as unknown and generics.

any is the type that opts a value out of type checking. You can assign anything to it, read any property from it, call it, and assign it to anything else, and the compiler stays silent. It exists so that untyped JavaScript can be adopted gradually.

Each any in your code is a hole in the safety net, and holes spread: a value read from an any is also any. Use unknown for values you cannot type yet, and keep any for rare, deliberate escapes.

TypeScript
let anything: any = 4;
anything = "now a string";
anything.nothing.at.all();   // compiles, throws at runtime

Try it

TypeScript any switches checking off

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Containing any with unknown and generics

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
let a: any;
function f(x: any): any {}
const list: any[] = [];
type Loose = { [key: string]: any };
Situation Result
Assign any value to any Always allowed.
Assign any to anything Allowed (except to never).
Property access, call, new, index on any Allowed; result is any.
Unannotated parameter or variable the compiler cannot infer Implicit any; an error under noImplicitAny.

Usage notes

  • noImplicitAny (part of strict) reports TS7006 ("Parameter 'x' implicitly has an 'any' type") instead of silently inferring any. Keep it on.
  • JSON and legacy APIs return any: JSON.parse and Response.json() both do. Immediately assign the result to a validated, typed shape or to unknown.
  • catch variables are unknown under useUnknownInCatchVariables, which strict enables.
  • Spreading. An any in a union absorbs the whole union: string | any is any.
  • Alternatives. unknown for "type not known yet"; generics for "same type in and out"; object or Record<string, unknown> for loose objects; never for unreachable branches.
  • Lint rules such as @typescript-eslint/no-explicit-any and no-unsafe-* find leaks; skipLibCheck hides them in .d.ts files only.
  • When it is acceptable: quick migration steps, tests of invalid input, and interop with genuinely dynamic code. Add a comment explaining why.
  • Assertions. value as any silences the compiler; prefer as unknown as T only when you really know better, and keep it small.