Parameter 'X' implicitly has an 'any' type in TypeScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Parameter 'X' implicitly has an 'any' type in TypeScript #

The "Parameter 'X' implicitly has an 'any' type" error occurs when a function's parameter has an implicit type of any. To solve the error, explicitly set the parameter's type to any, use a more specific type, or set noImplicitAny to false in tsconfig.json.

Here is an example of how the error occurs.

index.ts
// ⛔️ Error: Parameter 'a' implicitly // has an 'any' type.ts(7006) function sum(a, b) { return a + b; }

The parameters of the sum function were not explicitly typed, so TypeScript set them to have an implicit type of any.

One way to solve the error is to explicitly set the type of the parameters to any.

index.ts
function sum(a: any, b: any) { return a + b; } console.log(sum(10, 50)); // 👉️ 60
We explicitly typed the a and b parameters to have a type of any. The type is no longer implicitly set, so we don't get the error.

The same approach can be used with arrow functions, callbacks, etc.

index.ts
const sum = (a: any, b: any) => { return a + b; }; console.log(sum(10, 50)); // 👉️ 60

A much better solution is to be more specific when setting the type of the parameters.

index.ts
function sum(a: number, b: number) { return a + b; } console.log(sum(10, 50)); // 👉️ 60

Now the parameters in the sum function are explicitly typed to be numbers.

This is much better because the any type effectively turns off type checking in TypeScript.

If you don't like this behavior and want to suppress errors when values are implicitly typed as any, set the noImplicitAny option to false in your tsconfig.json file.

tsconfig.json
{ "compilerOptions": { // ... other properties "noImplicitAny": false } }

If the strict property is set to true, the noImplicitAny option defaults to true.

When TypeScript can't find type annotations for a value and they can't be inferred, it falls back to a type of any.

When you set noImplicitAny to true, TypeScript issues an error every time it can't find or infer type annotations for a value.

By turning it off, you suppress the error messages. However, the more strict you write your TypeScript code, the less likely it is you will get unexpected runtime errors.

Conclusion #

The "Parameter 'X' implicitly has an 'any' type" error occurs when a function's parameter has an implicit type of any. To solve the error, explicitly set the parameter's type to any, use a more specific type, or set noImplicitAny to false in tsconfig.json.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.