typescript 参数隐式具有“any”类型

ogq8wdun  于 2023-05-08  发布在  TypeScript
关注(0)|答案(9)|浏览(609)

我在一个typescript项目中使用visual studio代码,在那里我使用了一些第三方npm js库。其中一些没有提供任何ts类型(types.d.ts文件),所以每当我使用参数或变量而没有指定它们的类型时,vs code的linting就会显示以下错误:**参数隐式地具有'any'类型。**此外,ts不会编译。
我如何才能防止这种情况发生?

mutmk8jj

mutmk8jj1#

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

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

// enable this rule:
"noImplicitAny": false

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

npm install -g tslint

第三,安装tslint vs code extension

bweufnob

bweufnob2#

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

xlpyo6sf

xlpyo6sf3#

**我不想修改配置文件和dumbdown typescript!!

这段代码抛出了一个错误:

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

我通过添加一个“any”类型来修复我的:

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

mu0hgdu04#

我在这里结束了以下错误。这是必应上的第一个搜索结果,所以,把我的解决方案,如果它帮助别人。

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

我是这样解决的。
之前

const reportWebVitals = onPerfEntry  => {

之后

const reportWebVitals = (onPerfEntry : any) => {

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

gorkyyrv

gorkyyrv5#

对于那些:

  • 我在导入import mymodule from '@/path/to/mymodule.js'等js文件时遇到此问题
  • 不想把noImplicitAny设为false

您可以:

  • 创建名为index.d.ts的文件
  • 在那里放上
declare module '@/path/to/mymodule.js'
// or using wild card *, like `declare module '@/store/*.js'`
pdsfdshx

pdsfdshx6#

如果第三方库不提供类型,首先执行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类型定义的来源。

ct2axkht

ct2axkht7#

For error参数'dateText'隐式具有'any'类型。

作为一个起点,从字面上阅读错误,除非有其他更合适的解决方案,请使用any,如下所示:

TS错误:纯JSmyArray.find( x => x.someValue == comparisonValue)
TS成功:打印文本myArray.find( (x:any) => x.someValue == comparisonValue)
OP原问题:

错误代码如下所示

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

解决方案错误代码如下所示

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

yc0p9oo08#

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

  }
}
tnkciper

tnkciper9#

这对我来说是一个诡计。检查我如何使用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);

相关问题