Typescript Postmessage fails with one argument, and two

Viewed 1408

Have Typescript project.

When this: postMessage(result);

I get this..

Expected 2-3 arguments, but got 1.Expected 2-3 arguments, but got 1.

When this: postMessage(result, '*');

I get this..

Uncaught TypeError: Failed to execute 'postMessage' on 'DedicatedWorkerGlobalScope': Overload resolution failed.
    at runParser

What does Typescript want here? Is it one or two arguments? And why is this causing an issue?

2 Answers

If you are using MessageChannel's port postMessage function, then you have to pass only one parameter.

event.ports[0].postMessage(data)

If you are using window.postMessage, then you have to pass 2 or 3 parameters.

window.postMessage(data, origin, [transfer])

You're going to have to add webworker to your tsconfig.json or create a separate TS project for your worker, because postMessage has a different API in the worker and in the browser environment.

"compilerOptions" : {
    "lib": [
        "webworker"
Related