justin.m.chase
justin.m.chase

Reputation: 13655

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

Upvotes: 4

Views: 2703

Answers (2)

EuberDeveloper
EuberDeveloper

Reputation: 1048

Just an addition, in my case the typescript's version was much newer than 3.6 (namely 5.5.4), but I still was having the issue "error TS2503: Cannot find namespace 'WebAssembly"

The solution, as per this GH issue, was adding DOM to the compilerOptions.lib parameter of tsconfig.json

Upvotes: 1

Jorge Leitao
Jorge Leitao

Reputation: 20103

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(?)

Upvotes: 2

Related Questions