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.
let anything: any = 4; anything = "now a string"; anything.nothing.at.all(); // compiles, throws at runtime
Try it
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.
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
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 ofstrict) reports TS7006 ("Parameter 'x' implicitly has an 'any' type") instead of silently inferringany. Keep it on.- JSON and legacy APIs return
any:JSON.parseandResponse.json()both do. Immediately assign the result to a validated, typed shape or tounknown. catchvariables areunknownunderuseUnknownInCatchVariables, whichstrictenables.- Spreading. An
anyin a union absorbs the whole union:string | anyisany. - Alternatives.
unknownfor "type not known yet"; generics for "same type in and out";objectorRecord<string, unknown>for loose objects;neverfor unreachable branches. - Lint rules such as
@typescript-eslint/no-explicit-anyandno-unsafe-*find leaks;skipLibCheckhides them in.d.tsfiles 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 anysilences the compiler; preferas unknown as Tonly when you really know better, and keep it small.