【问题标题】:TypeScript not finding the types in @typesTypeScript 在@types 中找不到类型
【发布时间】:2019-01-03 00:59:14
【问题描述】:

我正在尝试在我的项目中从@types/webgl2 导入一些类型。 我遵循了这里提到的每一条建议: TypeScript typings give me "index.d.ts is not a module"

将此添加到顶部:import 'webgl2';

  • 我已确定“modeResolution”在tsconfig.json 中是“true
  • 我在tsconfig.json 中添加了一行"types":["webgl2"]
  • package.json 中将"@types/webgl2": "0.0.4" 添加到devDependenciesdependencies
  • 是否在我的项目文件夹中安装了 npm:
npm WARN package.json webgl@0.0.1 No README data
npm WARN package.json Dependency '@types/webgl2' exists in both dependencies and devDependencies, using '@types/webgl2@0.0.4' from  dependencies

我可以很容易地在node_modules/@types 中看到webgl2,并且我还可以在下面看到所有错误符号的声明:

declare var WebGl2RenderingContext {

我仍然收到这些错误:

(multiple lines)
Cannot find name 'WebGl2RenderingContext'. Did you mean 'WebGL2RenderingContext'?

对应于:

const format = WebGl2RenderingContext.RED;

【问题讨论】:

    标签: node.js typescript webgl2


    【解决方案1】:

    对我来说,我遇到了一个问题,在 tsconfig 中指定了 types,因此某些类型不会自动添加到全局范围。

    所以有问题的包是@types/dom-webcodecs,我像这样将它添加到 tsconfig 中

    {
      "compilerOptions": {
        "types": [
          "dom-webcodecs",
          ...
        ]
      }
    }
    

    然后我可以访问这些类型。

    这与另一个答案谈到的 typeRoots 不同。如果需要,您可以探索差异here

    【讨论】:

      【解决方案2】:

      对于 NG9'ers,请尝试删除您的 package-lock.json 文件,然后:

      npm i crypto
      

      【讨论】:

        【解决方案3】:

        在你的 tsconfig 中设置 typeRoot:

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

        【讨论】:

          【解决方案4】:

          通过修正拼写错误解决。

          【讨论】:

          • 年幼的孩子们总是说“你必须擅长数学才能成为脚本语言程序员吗?”。我的回答总是“不,只是打字和拼写”。
          猜你喜欢
          • 2016-12-29
          • 2018-07-11
          • 2016-09-29
          • 1970-01-01
          • 2019-03-02
          • 2016-03-20
          • 1970-01-01
          • 1970-01-01
          • 2017-02-01
          相关资源
          最近更新 更多