noImplicitAny
noImplicitAny is the compiler flag that errors when TypeScript would otherwise silently give a value the any type. It does not ban any you wrote on purpose.
Detailed Explanation
When the compiler cannot infer a type, its fallback used to be any. An unannotated parameter, a callback argument the checker could not see, a JSON value flowing through an untyped helper — all of them became any, and any turns checking off. The program compiled. The types were fiction.
noImplicitAny changes the fallback into an error: "Parameter 'x' implicitly has an 'any' type." You then annotate the parameter, add a generic, or fix the context so inference works. The flag is part of strict. New projects get it from tsc --init.
What it catches:
- Function parameters with no annotation and no contextual type (
function f(x) {}) - Some rest and destructured parameters in the same situation
thisin a function wherenoImplicitThisalso applies (a sibling flag)- Variables whose only "type" came from an implicit
anyelsewhere
What it does not catch:
- Explicit
: any as anyanyreturned by@typespackages or byJSON.parse- Defaults like
<T = any> - Values typed
anybecause a dependency's types said so
That list is why a project can have noImplicitAny on and still be full of holes. The flag stops silent any. It does not stop chosen any. A lint rule (@typescript-eslint/no-explicit-any) is the usual next step if you want the explicit form banned too.
The fix is almost always an annotation at the boundary. Parameters of exported functions should be annotated anyway. Callbacks passed to typed APIs (array.map, addEventListener) already have a contextual type, so they do not need an annotation and do not trip the flag. The errors you see are the functions you wrote that nobody typed.
On a JavaScript file checked with allowJs and checkJs, the same flag applies, and the escape hatch is a JSDoc @param {string} x or a // @ts-check plus annotations. noImplicitAny is often the first flag people enable on a JS migration, because the errors point at the functions that need signatures.
The cost is a burst of annotations when you turn it on. The benefit is that every remaining any is visible in the source — a comment-worthy decision, not an accident of inference.
Code Examples
1. The error, and the annotation that fixes it.
// 1. The error, and the annotation that fixes it.
// function slug(input) {
// return input.toLowerCase();
// }
// Error: Parameter 'input' implicitly has an 'any' type.
function slug(input: string): string {
return input.toLowerCase();
}
console.log(slug("Hello")); // hello2. Contextual typing means no annotation is required.
// 2. Contextual typing means no annotation is required.
const names = ["ada", "grace"];
const lengths = names.map((name) => name.length); // name is string, no implicit any
document.addEventListener("click", (event) => {
console.log(event.clientX); // event is MouseEvent
});3. The flag in tsconfig, including a JS migration setup.
// 3. The flag in tsconfig, including a JS migration setup.
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"allowJs": true,
"checkJs": true
}
}Common Gotchas
Explicit `any` still compiles
the flag is not a ban. Search for `: any` and `as any` if you want the real hole count.
Destructuring does not annotate the pieces
`function f({ name: string })` *renames* `name` to `string`. Annotate the pattern: `function f({ name }: { name: string })`.
Callbacks of typed functions are already contextual
if `noImplicitAny` fires inside a `map` callback, the array itself is probably `any[]`.
A generic default of `any` bypasses the flag
`<T = any>` is an explicit any at every unspecified call site. Prefer `unknown`.