TypeScript-DOM-lib-generator icon indicating copy to clipboard operation
TypeScript-DOM-lib-generator copied to clipboard

WebWorker postMessage overload wrong for transferrables

Open tknight-dev opened this issue 1 year ago • 3 comments

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

tknight-dev avatar Nov 19 '24 16:11 tknight-dev

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

saschanaz avatar Nov 29 '24 21:11 saschanaz

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.

tknight-dev avatar Dec 02 '24 18:12 tknight-dev

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

saschanaz avatar Dec 02 '24 18:12 saschanaz