Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebWorker postMessage overload wrong for transferrables #1849

Open
tknight0000 opened this issue Nov 19, 2024 · 3 comments
Open

WebWorker postMessage overload wrong for transferrables #1849

tknight0000 opened this issue Nov 19, 2024 · 3 comments

Comments

@tknight0000
Copy link

tknight0000 commented Nov 19, 2024

Main:

let worker: Worker = new Woker(new URL('myWorker', import.meta.url));
...

Worker:

self.onmessage = (event: MessageEvent) => {

   let test: ArrayBuffer = new ArrayBuffer(10);

   (<any>self).postMessage(test, [test]); // Works

   self.postMessage(test, [test]); // Compilation Error (false negative)

   self.postMessage(test, '*', [test]); // Fails to send
});

Erroneous Compilation Error:

error TS2769: No overload matches this call.
Overload 1 of 2, '(message: any, targetOrigin: string, transfer?: Transferable[] | undefined): void', gave the following error.
Argument of type 'ArrayBuffer[]' is not assignable to parameter of type 'string'.
Overload 2 of 2, '(message: any, options?: WindowPostMessageOptions | undefined): void', gave the following error.
Type 'ArrayBuffer[]' has no properties in common with type 'WindowPostMessageOptions'.

self.postMessage(test, [test]);

Typescript Version: 5.6.3

@saschanaz
Copy link
Contributor

Sounds like you should override the lib with webworker: https://www.typescriptlang.org/tsconfig/#lib

@tknight0000
Copy link
Author

Are you suggesting I create a tsconfig that targets the webworkers lib to compile the workers separately from the current project's tsconfig that specifically targets es6? With excludes and includes set with *.worker.* (arbitrary) file naming conventions to separate compilation processes?

If so, that's double the -watch instances on top of the configs. Just to prevent TS from throwing a bad error message.

@saschanaz
Copy link
Contributor

That's probably the way for now, because otherwise there's no way to tell the worker file to not use the same context with other files. Having a way to tell the compiler that "hey this file is a single file worker, give it its own context, thank you" with an inline way, but I don't think that exists. cc @sandersn to confirm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants