参数隐式具有 'any' 类型

parameter implicitly has an 'any' type

我正在为打字稿项目使用 visual studio 代码,我在其中使用了一些第 3 方 npm js 库。其中一些不提供任何 ts 类型(types.d.ts 文件),所以每当我使用参数或变量而不指定它们的类型时,vs 代码的 linting 都会显示此错误:parameter implicitly has an 'any'类型。 另外,ts 不会编译。

如何防止这种情况发生?

首先,要使 typescript 容忍参数而不声明其类型,请编辑 tsconfig.json

// disable this rule:
// "strict": true,

// enable this rule:
"noImplicitAny": false

其次,安装 tslint npm 包作为 tslint vs 代码扩展的先决条件

npm install -g tslint

三、安装tslint vs code extension

指定类型:例如(callback:any) => { }

** 我不想修改配置文件和简化打字稿!!

此代码向我抛出错误:

  onDelete(todo) {
    console.log('delete')
    this.deleteTodo.emit(todo)   
  }

我通过添加“任意”类型修复了我的问题:

  onDelete(todo: any) {
    console.log('delete')
  this.deleteTodo.emit(todo)   
  }

对于那些:

  • 在导入 js 文件如 import mymodule from '@/path/to/mymodule.js'
  • 时出现此问题
  • 并且不想将 noImplicitAny 变为 false

您可以:

  • 创建一个名为 index.d.ts
  • 的文件
  • 把类似的东西放在那里
declare module '@/path/to/mymodule.js'
// or using wild card *, like `declare module '@/store/*.js'`

我在这里遇到了以下错误。这是 Bing 上的第一个搜索结果,所以,如果对某人有帮助,请提出我的解决方案。

参数 'onPerfEntry' 隐式具有 'any' 类型。 TS7006

我是这样解决的

之前

const reportWebVitals = onPerfEntry  => {

之后

const reportWebVitals = (onPerfEntry : any) => {

我理解这是一件简单的事情,但对于像我这样的初学者来说,我花了一段时间才弄明白。

这对我有用。 检查我如何使用 x 变量

   From

     const user = users.find(x => x.username === username && x.password === password);

   To
const user = users.find((x:any) => x.username === username && x.password === password);
import { Component, OnInit } from '@angular/core';


@Component({
  selector: 'app-info',
  templateUrl: './info.component.html',
  styleUrls: ['./info.component.css']
})
export class InfoComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {


  }
  onNameChange(ngModelObj){
    console.log(ngModelObj);
    
    enter code here

  }
}

错误信息: 参数 'dateText' 隐式具有 'any' 类型。

Error Code look like below

$(() => {
        $('#datepicker').datepicker({
          changeMonth: true,
          changeYear: true,
          yearRange: '1920:2099',
          onSelect: (dateText) => {
            this.dob.setValue(dateText);
          },
        });
      });

Solution error code looks like below

$(() => {
        $('#datepicker').datepicker({
          changeMonth: true,
          changeYear: true,
          yearRange: '1920:2099',
          onSelect: (dateText:any) => {
            this.dob.setValue(dateText);
          },
        });
      });

如果第三方库不提供类型,首先执行 npm 搜索 @types/SOMELIBRARY(将 SOMELIBRARY 替换为模块的 npm 名称):

npm search @types/SOMELIBRARY

如果存在,npm 安装它:

npm install @types/SOMELIBRARY

这是为不自己做的项目定义的类型的存储库。如果那里没有类型定义,你可以自己定义类型:

// yourmodule.d.ts
declare module "yourmodule" {
  export default function somefunction(...): T
}

Typescript 应该找到并使用它来声明 yourmodule 中的类型。

查看此处了解更多信息:https://www.typescriptlang.org/docs/handbook/declaration-files/library-structures.html

您可以在此处简单地将 yourmodule 的所有部分定义为 any,作为临时解决方案,您可以随时轻松添加更多详细信息。如果你最终得到一个非常漂亮且可用的 yourmodule.d.ts,请考虑将其添加到 DefinitelyTyped:https://github.com/DefinitelyTyped/DefinitelyTyped 这就是上面 @types/LIBRARY 类型定义的来源。