When using the --noImplicitAny
compiler option, warnings are generated only where type inference is unsuccessful, leading to a fallback or default to any
. For instance, an implicit-any
error occurs when a function parameter cannot be contextually typed:
const f = (arg) => arg + 1; // error!
// ------> ~~~ implicit any
This error also appears if a variable that should be auto-typed (microsoft/TypeScript#11263) cannot be inferred by the compiler through control flow, like in cases where the variable is referenced in a separate function scope:
let w; // error!
// >~ implicit any
w = 2;
function foo() { w } // implicit any
In these instances, the compiler indicates uncertainty by assigning an any
type.
In contrast, calling a function with a return type of any
results in a value of type any
. While this may seem like an "implicit" assignment, it does not stem from a failure of type inference. When an assigned variable is successfully inferred as any
, similar to how x
being a number
in const x = 1 + 2
demonstrates successful inference, there are no implicit-any
errors present.
Moreover, since the TypeScript library typings for JSON.parse()
appear as:
interface JSON {
parse(text: string, reviver?: (this: any, k: string, v: any) => any): any;
}
the code snippet:
const b = JSON.parse('{"a":"x"}');
successfully infers any
for the type of b
without triggering a compiler warning.
Click here for the Playground link to view the code