-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Worker.postMessage missing optional options parameter #31448
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
Worker.postMessage missing optional options parameter #31448
Comments
The problem is that MessagePort has two overloads for On the other hand, reading through the spec leads me to this interface: interface DedicatedWorkerGlobalScope : WorkerGlobalScope {
[Replaceable] readonly attribute DOMString name;
void postMessage(any message, sequence<object> transfer);
void postMessage(any message, optional PostMessageOptions options);
void close();
attribute EventHandler onmessage;
attribute EventHandler onmessageerror;
};
|
I'm confused though: despite the non-optional |
@fatcerberus I believe the code that creates a signature from a union of signatures is stricter about the number of required parameters in the resulting signature. Here's a standalone version that's easier to paste into the playground: interface Terable {
a,b,c
}
interface PostMessageOptions {
transferable: Terable[]
}
interface MsgPort {
postMessage(message: any, options?: PostMessageOptions): void;
postMessage(message: any, transfer: Terable[]): void;
}
interface Wrkr {
postMessage(message: any, transfer?: Terable[]): void;
}
declare let either: MsgPort | Wrkr;
either.postMessage('test') |
This is a regression from 3.4. Is there a reason that MessagePort was updated but Worker was not? 3.4:
3.5:
The error goes away if |
Looks like @saschanaz just missed it in microsoft/TypeScript-DOM-lib-generator#679. That PR has overrides for MessagePort, ServiceWorker and DedicatedWorkerGlobalScope, but not Worker. I think it's correct to add it for Worker too, but I want to find out how @saschanaz determined those 3 were the ones that got the overrides before I do. |
Here's the link for Worker. I'll go ahead and add it in TSJS-lib-generator. |
@sandersn Maybe the transfer param should be optional? I found this in the spec:
And now I still got an error while pass only one param, I have to change it to worker.postMessage('message', undefined); that is not a good practice |
TypeScript Version: 3.5.0-rc
Search Terms:
Worker MessagePort postMessage
Code
Expected behavior:
No error. There is no error in 3.4. I'm not sure if this is a bug in the compiler or due to the changes to MessagePort.postMessage in lib.dom.d.ts. Should Worker.postMessage also be updated to have the optional options parameter?
Reference: https://html.spec.whatwg.org/multipage/workers.html#dedicated-workers-and-the-worker-interface
Actual behavior:
error TS2554: Expected 2 arguments, but got 1.
The text was updated successfully, but these errors were encountered: