日本語

How to Fix TS7006 “Parameter implicitly has an ‘any’ type” in TypeScript

​​TypeScript​Troubleshooting

In TypeScript, if you leave a function parameter without a type, you get error TS7006: Parameter 'price' implicitly has an 'any' type. With "strict": true, a parameter whose type cannot be inferred and becomes any is reported as an error. Writing the type on the parameter, such as price: number, clears the error. You usually see this when you move JavaScript code into .ts files, or when you start writing new code with the settings made by tsc --init.

In my test environment, I triggered this error with both tsc and the TypeScript language server, and I confirmed the full message and that adding a type removes it.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, and TypeScript 7.0.2. For comparison, I also ran tsc on the same files with TypeScript 5.9.3. The tsconfig.json was generated with npx tsc --init and includes "strict": true. I checked the errors that editors such as VS Code show by running the TypeScript 7 language server (tsc --lsp --stdio) and the TypeScript 5.9.3 tsserver directly. I did not check how the errors look in an editor window.

The full error

In src/price.ts, I wrote a function that calculates a price including tax. The parameters price and rate have no types.

ts
export function addTax(price, rate) {
  return Math.round(price * (1 + rate));
}

console.log(addTax(1000, 0.1));

Running npx tsc --noEmit produced the following output (exit code 1).

text
src/price.ts(1,24): error TS7006: Parameter 'price' implicitly has an 'any' type.
src/price.ts(1,31): error TS7006: Parameter 'rate' implicitly has an 'any' type.

This means "the type of the parameter price is implicitly any." One error is reported for each parameter without a type, so there are two here. With --pretty, the location is shown with a squiggly underline (I removed the control characters used for color).

text
src/price.ts:1:24 - error TS7006: Parameter 'price' implicitly has an 'any' type.

1 export function addTax(price, rate) {
                         ~~~~~

src/price.ts:1:31 - error TS7006: Parameter 'rate' implicitly has an 'any' type.

1 export function addTax(price, rate) {
                                ~~~~


Found 2 errors in the same file, starting at: src/price.ts:1

The same error appears in the language server

The TypeScript 7 tsc runs as a language server when you pass --lsp --stdio (I started it in the test environment to confirm this). I had this language server open src/price.ts using the same steps an editor uses (initialize, then didOpen). I used a small script of my own to print the diagnostics it returned and the hover information (the type) for price on line 1. The output was:

text
server: typescript-go 7.0.2
diagnostics (2):
  1:24-1:29 severity=1 source=ts code=7006 Parameter 'price' implicitly has an 'any' type.
  1:31-1:35 severity=1 source=ts code=7006 Parameter 'rate' implicitly has an 'any' type.
hover 1:24: (parameter) price: any

severity=1 is the value that the LSP specification uses for an error. The message text and positions match tsc, and price has the type any.

noImplicitAny, which is included in strict, is raising the error

When TypeScript cannot determine the type of a parameter that has no type annotation, it treats that parameter as type any. Values of type any are not type-checked. According to the tsconfig reference for noImplicitAny (in English), enabling noImplicitAny makes locations that would become any into errors. TS7006 is one of these errors.

noImplicitAny is one of the settings that are enabled together when you turn on strict. In the TypeScript 5.9.3 compiler, the definition of this setting has strictFlag: true, and its default is described as false_unless_strict_is_set (false unless strict is set).

In my test environment, running tsc with --noImplicitAny false produced no errors. Running it with --strict false --exactOptionalPropertyTypes false also produced no errors. I disabled exactOptionalPropertyTypes together with strict because it is also in the settings from tsc --init. However, the parameters were still any. When I added "noImplicitAny": false to tsconfig.json and opened the file in the language server, the hover type was still (parameter) price: any. The diagnostics changed to these two:

text
  1:24-1:29 severity=4 source=ts code=7044 Parameter 'price' implicitly has an 'any' type, but a better type may be inferred from usage.
  1:31-1:35 severity=4 source=ts code=7044 Parameter 'rate' implicitly has an 'any' type, but a better type may be inferred from usage.

severity=4 is the value the LSP specification uses for a Hint, not an error. With tsc and --noImplicitAny false on the command line, nothing was reported.

Adding a type to the parameter removes the error

The fix is to write the type of the value each parameter receives.

diff
-export function addTax(price, rate) {
+export function addTax(price: number, rate: number) {

As described in the handbook section on parameter type annotations (in English), you write the type after the parameter name in the form : type. When I ran npx tsc --noEmit on the fixed file, it finished with exit code 0 and no output. The language server reported zero diagnostics, and the hover type changed to (parameter) price: number. When I converted the file to JavaScript and ran it with Node.js, it printed 1100.

With TypeScript 5.9.3, the original file produced the same two TS7006 errors at the same positions, and the fixed file had no errors. However, the exit code for tsc when there are errors was 2 with 5.9.3 and 1 with 7.0.2.

Writing any still clears the error, but it misses incorrect calls

If you write price: any, rate: any, TS7006 also goes away. That is because you wrote any yourself rather than it being implicit. However, this turns off type checking. To compare, I wrote one call that passes the string "1000円" on line 5 to a function declared with any and to a function declared with number (the number version is in src/fix-call.ts):

ts
console.log(addTax("1000円", 0.1));

The file with any produced no errors from tsc, and when I converted it to JavaScript and ran it, it printed NaN. The file with number produced this error from tsc:

text
src/fix-call.ts(5,20): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.

This means "a value of type string cannot be passed to a parameter of type number." When you declare a specific type for a parameter, you can also catch mistakes at the call site.

Adding a type with an editor quick fix

The tsserver in TypeScript 5.9.3 (a server for editors that lets you use the compiler and language services through JSON messages; see the TypeScript wiki (in English)) has a quick fix that resolves TS7006. I opened the file before the fix and requested getCodeFixes at the error position. It returned this candidate:

text
codeFixes (1):
  inferFromUsage: Infer parameter types from usage
    1:29-1:29 -> ": number"
    1:35-1:35 -> ": number"

This is the candidate "Infer parameter types from usage." Based on how the parameters are used in the function (price * (1 + rate)), it inserts : number after price and rate.

On the other hand, when I requested a quick fix (textDocument/codeAction) at the same position from the TypeScript 7.0.2 language server, it returned zero candidates.

Some parameters without types do not raise errors

Not every parameter without a type raises an error. In my test environment, TS7006 did not appear for the following code.

ts
const prices = [1000, 2500];
const withTax = prices.map((price) => Math.round(price * 1.1));
console.log(withTax);

document.addEventListener("click", (event) => {
  console.log(event.clientX);
});

On hover, the type of price in the function passed to map was (parameter) price: number, and the type of event in the function passed to addEventListener was (parameter) event: PointerEvent. The parameter types are determined by the type of the function that receives them (map on number[], or the "click" event). The handbook calls this contextual typing.

A function stored in a variable first raises errors

Even with the same arrow function, storing it in a variable before passing it raised TS7006.

ts
const onClick = (event) => {
  console.log(event.clientX);
};

document.addEventListener("click", onClick);
text
src/handler.ts(1,18): error TS7006: Parameter 'event' implicitly has an 'any' type.

At the point of const onClick = ..., the compiler does not know where the function will be passed, so the type of event cannot be determined. Writing (event: PointerEvent) => { ... } removed the error. The same thing happens when you store an arrow function in a variable and use it directly, such as export const double = (n) => n * 2;, which produced Parameter 'n' implicitly has an 'any' type.

A parameter with a default value gets its type from that value

When you give a parameter a default value, as in function addTax(price: number, rate = 0.1), it did not raise an error even without a type annotation. The hover type was (parameter) rate: number, so the type was determined from the default value 0.1.

Destructured parameters produce TS7031

If you receive a parameter by destructuring an object, the error number and message change.

ts
export function greet({ name }) {
  return `こんにちは、${name}さん`;
}
text
src/destructure.ts(1,25): error TS7031: Binding element 'name' implicitly has an 'any' type.

This means "the type of name, taken out by destructuring, is implicitly any." In this case, write the type of the whole object after { name }. Writing function greet({ name }: { name: string }) removed the error.

​Posted by 86