添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
瘦瘦的小熊猫  ·  Three.js ...·  1 年前    · 
← Concepts

Parameter X implicitly has an 'any' type

This error occurs when you fail to add a type annotation to a parameter in a function.

const addTwoNumbers = (a, b) => {
Parameter 'b' implicitly has an 'any' type.7006
Parameter 'b' implicitly has an 'any' type.
Parameter 'a' implicitly has an 'any' type.7006
Parameter 'a' implicitly has an 'any' type. return a + b;

Why Is It Happening?

This is happening because of a setting that most TypeScript projects have turned on - strict mode.

// tsconfig.json
  "compilerOptions": {
    "strict": true

Strict mode tells TypeScript to add more checks to your code to make sure it's safe. One of these checks is to make sure that all parameters have a type annotation.

Pretty much all modern TypeScript projects use strict mode, so you'll see this error a lot.

Why Is This Error Part Of Strict Mode?

TypeScript can't infer the type of a parameter if you don't give it a type annotation. In the example above, TypeScript can't infer from usage what the type of a and b should be.

Adding two parameters together could be a number, sure. But it could be that you want to concatenate two strings together, which is also possible with the + operator.

const concatTwoStrings = (a, b) => {
Parameter 'b' implicitly has an 'any' type.7006
Parameter 'b' implicitly has an 'any' type.
Parameter 'a' implicitly has an 'any' type.7006
Parameter 'a' implicitly has an 'any' type. return a + b;

So, in strict mode TypeScript demands that you add a type annotation to all parameters. This is to make sure your function is called correctly:

const addTwoNumbers = (a: number, b: number) => {
  return a + b;
addTwoNumbers(1, 2); // OK
addTwoNumbers("1", "2"); // Error!
Argument of type 'string' is not assignable to parameter of type 'number'.2345
Argument of type 'string' is not assignable to parameter of type 'number'.

What Is An Implicit Any?

If you don't have strict mode turned on, TypeScript will automatically assign the any type to parameters that don't have a type annotation.

const addTwoNumbers = (a, b) => {
  return a + b;
const result = addTwoNumbers(1, 2);
const result: any

This is bad, because any disables type checking on anything it's applied to. We can begin really messing up if we're not careful:

const addTwoNumbers = (a, b) => {
  return a + b;