typescriptwebassembly

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. I am running in nodejs v11 and using 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.

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?

Here is my current tsconfig.json file:

{
    "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"]
}

Solution

  • This happens because the type declarations for webassembly are only available from typescript >3.4, as per this comment.

    The solution is to bump typescript to at least 3.6(?)