简体   繁体   English

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

[英]error TS2304: Cannot find name 'WebAssembly'

I probably have a TypeScript configuration issue but for the life of me I can't figure it out.我可能有一个 TypeScript 配置问题,但在我的生活中我无法弄清楚。 I am running in nodejs v11 and using TypeScript v3.4.5.我在 nodejs v11 中运行并使用 TypeScript v3.4.5。

I am simply attempting to compile the following code with typescript:我只是试图用打字稿编译以下代码:

const { Module, Instance } = WebAssembly;

Which generates the error:这会产生错误:

error TS2304: Cannot find name 'WebAssembly'.

What's strange is if I hover over all 3 types in VSCode it properly resolves the Types and shows me no errors.奇怪的是,如果我将鼠标悬停在 VSCode 中的所有 3 种类型上,它会正确解析类型并且不会显示任何错误。

If I use this workaround, it compiles and runs successfully:如果我使用此解决方法,它会成功编译并运行:

const { Module, Instance } = (global as any).WebAssembly;

What do I have to do to get TS to recognize WebAssembly correctly?我该怎么做才能让 TS 正确识别 WebAssembly?

Here is my current tsconfig.json file:这是我当前的tsconfig.json文件:

{
    "compilerOptions": {
      "inlineSourceMap": true,
      "noUnusedLocals": true,
      "outDir": "build",
      "target":"es2018",
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true,
      "esModuleInterop": true,
      "module": "commonjs",
      "moduleResolution": "node",
      "noUnusedParameters": false

    },
    "exclude": ["node_modules"],
    "include": ["src/**/*.ts"]
  }

EDIT: It works with this config:编辑:它适用于这个配置:

{
  "compilerOptions": {
    "removeComments": true,
    "sourceMap": true,
    "target": "es6",
    "module":"commonjs",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "lib": ["es2018", "dom"],
    "outDir": "build",
    "sourceRoot": "src",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "include": ["**/*.ts"],
  "exclude": ["**/node_modules"]
}

This happens because the type declarations of for webassembly are only available from typescript >3.4, as per this comment .发生这种情况是因为 webassembly 的类型声明仅在 typescript >3.4 中可用,根据此注释

The solution is to bump typescript to at least 3.6(?)解决方案是将打字稿至少提高到 3.6(?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM