Prevent accidental implicit any
Enforce explicit types for unannotated parameters and declarations in new modules.
TypeScript · Const
noImplicitAny makes TypeScript report errors when it would infer any because a value lacks an annotation or usable contextual type. Enable it in compilerOptions to prevent accidental loss of type checking.
Enable this compiler option to make TypeScript report locations where it would otherwise infer the type any. In large codebases, annotate those values or provide enough contextual typing to preserve static checking instead of silently accepting unchecked operations.
{"compilerOptions":{"noImplicitAny":true}}| Name | Type | Description |
|---|---|---|
noImplicitAny | boolean | Boolean compiler option. When true, reports declarations and expressions whose type would otherwise be inferred as any. |
{
"compilerOptions": {
"noImplicitAny": true
}
}
function formatUser(id) {
return `User ${id}`;
}With noImplicitAny enabled, the unannotated parameter causes a compile-time error; add an explicit type to make the function valid.
Enforce explicit types for unannotated parameters and declarations in new modules.
Detect unchecked member access that would otherwise compile through an inferred any.
Adopt stronger checking incrementally by enabling the option in a project configuration.
It reports an error whenever TypeScript would infer any because no annotation or contextual type determines the value's type. This prevents unchecked operations from passing silently.
Follow-up: How can a project enable the related strictness settings together? Set strict to true, then opt out of individual strictness flags when necessary.
A parameter such as function fn(s) can receive an implicit any error when TypeScript cannot infer s. Giving s an explicit type resolves that diagnostic and restores checking for operations on it.
Follow-up: What happens if the parameter is explicitly declared as any? The explicit annotation is intentional and is not an implicit-any inference diagnostic.
The setting belongs in compilerOptions in tsconfig.json, where it can be enabled with a boolean value. It can also be enabled through the corresponding compiler command-line option.
Follow-up: Why is this useful in a large codebase? It turns accidental loss of type information into an actionable compile-time error.
strictstrictNullChecksnoImplicitThisany