4

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"]
}
4
  • I have the same problem. Some advice to add "lib": [ "dom" ] to compilerOptions, but it didn't work for me. Commented Jul 29, 2019 at 16:02
  • Yeah that did seem to fix it. I'll update the question with a working config, and if it fixes it for you you can submit answer and I'll mark it as the right answer. Commented Jul 30, 2019 at 22:35
  • With this config I now get Property 'instantiateStreaming' does not exist on type 'typeof WebAssembly'. Commented Aug 2, 2019 at 15:18
  • It looks like that function isn't available in nodejs yet. Perhaps remove the "es2018" from "lib"? I'm not sure. Commented Aug 2, 2019 at 19:14

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.