【问题标题】:error TS2304: Cannot find name 'InputEvent'错误 TS2304:找不到名称“InputEvent”
【发布时间】:2019-05-06 18:06:34
【问题描述】:

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

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

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

谢谢!

【问题讨论】:

    标签: angular typescript typescript-typings


    【解决方案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

    【讨论】:

    • 几年过去了,我终于得到了答案,哈哈。我认为答案是有道理的,所以我接受它,但我目前没有从事该项目,而且我太忙无法验证它。我突然想到,Firefox 在InputEvent 中并不原生支持event.data。该软件包是否为 Firefox 提供了应有的行为?
    • 我的情况和你的不一样,我就是搞不定... =/
    猜你喜欢
    • 2016-12-18
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    相关资源
    最近更新 更多