错误TS2304:找不到名称“ InputEvent”

时间:2018-12-05 08:08:59

标签: angular typescript typescript-typings

Does TypeScript have type definitions for InputEvent?中所述,我尝试使用@types/dom-inputevent在Angular 7项目中获取InputEvent。但是我每次使用时都会显示error TS2304: Cannot find name 'InputEvent'

是否需要在Angular项目中“注册”它还是一个错误?

如果没有可用的修复程序,我只想以一种便捷的方法来获得与event.data中的InputEvent相同的结果。

谢谢!

1 个答案:

答案 0 :(得分:1)

我在尝试使用Angular 6应用程序的单元测试中使用@ types / dom-inputevent时遇到了相同的问题。就我而言,原因是我在tsconfig.spec.js中有一个明确的“类型”条目,如下所述。

tsconfig.json (省略了无关的部分)

{
  "compilerOptions": {
     "typeRoots": [
       "node_modules/@types"
     ]
  }
}

tsconfig.spec.js (省略了不相关的部分)

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
  "types": [
    "jasmine",
    "node"
  ]
}

如您所见,有一个types条目列出了在编译过程中应提供的显式类型。这会覆盖父tsconfig.json中的typeRoots设置,因此在编译单元测试时,只有@ types / jasmine和@ types / node可用。另请参见此处:https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types

解决方案是将dom-inputevent添加到types列表中,例如

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
  "types": [
    "jasmine",
    "node",
    "dom-inputevent"
  ]
}

另一种方法是完全删除类型条目。如果这样做的话,typeRoots会加入进来,并在node_modules / @ types下查找所有类型,因此也包括@types/dom-inputevent