WebWorker postMessage overload wrong for transferrables
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
Sounds like you should override the lib with webworker: https://www.typescriptlang.org/tsconfig/#lib
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.
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