You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Where there are overloads of a method, the method cannot be directly called with a parameter of a union type that matches the available overloads.
Here's a cut-down example that highlights the apparent issue (can be pasted into TypeScript playground):
interfaceJQuery{append(content1: JQuery): JQuery;append(content1: string): JQuery;}varmessageElement: JQuery;functionsetMessage(message: string|JQuery){messageElement.append(message);// error JQuery is not assignable to type string// Help the compiler out with a type guard, it's okayif(typeofmessage==="string"){messageElement.append(message);}else{messageElement.append(message);}}
It's clear why the heavily-redundant type-guard workaround makes it easier for the compiler: in the general case each overload is free to return a different type, e.g. hypothetically weird example:
By writing it with union types, we don't capture the fact that JQuery goes to boolean, whereas string goes to number, but string never goes to boolean, etc..
But what I'm hoping is that compiler could behave as if there was an additional "catch all" overload that accepted the union of the parameters and return the union of the return types, i.e.:
interfaceJQuery{append(content1: JQuery): boolean;append(content1: string): number;// Therefore the compiler infers the existence of this extra overload:append(content1: JQuery|string): boolean|number;}
That way, union type parameters would go into overloads more smoothly.
The text was updated successfully, but these errors were encountered:
Where there are overloads of a method, the method cannot be directly called with a parameter of a union type that matches the available overloads.
Here's a cut-down example that highlights the apparent issue (can be pasted into TypeScript playground):
It's clear why the heavily-redundant type-guard workaround makes it easier for the compiler: in the general case each overload is free to return a different type, e.g. hypothetically weird example:
This is an example of overloads capturing a specific relation that would be obscured if we instead used union types:
By writing it with union types, we don't capture the fact that JQuery goes to boolean, whereas string goes to number, but string never goes to boolean, etc..
But what I'm hoping is that compiler could behave as if there was an additional "catch all" overload that accepted the union of the parameters and return the union of the return types, i.e.:
That way, union type parameters would go into overloads more smoothly.
The text was updated successfully, but these errors were encountered: