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
Search Terms: function overload type interface union
Code
// function overloadfunctionnav(x: (to: string,from1: string,next: number)=>void): void;functionnav(x: (to: string,from1: string)=>void): void;functionnav(x: any): any{returntrue;}nav((to,from,nex)=>{})// worksnav((to,from)=>{})// works// using typesexporttypeNavType=|((to: string,from1: string,next: number)=>void)|((to: string,from1: string)=>void)constt1: NavType=(to,from,next)=>{};// worksconstt2: NavType=(to,from)=>{};// doesn't work// using interface with overloadsinterfaceInterfaceType{(to: string,from1: string,next: number): void(to: string,from1: string): void}consti1: InterfaceType=(to,from,next)=>{};// worksconsti2: InterfaceType=(to,from)=>{};// doesn't work
Expected behavior:
The argument inference should be the same when using type/interface or the function overload.
Actual behavior: Union type fails to infer the 2 argument function Interface fails to infer the overloads, if we comment one of the overloads it will correctly infer 1 or both.
There's no reason to write overloads like this if you're intending to accept a callback; just take the 3-argument version.
In the cases where you can't just take the 3 argument version, the problem becomes apparent - TS would have to typecheck every possible way the callback might get called, which would open the door to a combinatorial explosion.
The overloaded version nav will just always take the top overload that matches, which isn't the case for union types or assignments.
TypeScript Version: 3.9.x && 4.0.0.beta
Search Terms: function overload type interface union
Code
Expected behavior:
The argument inference should be the same when using type/interface or the function overload.
Actual behavior:
Union type
fails to infer the 2 argument functionInterface
fails to infer the overloads, if we comment one of the overloads it will correctly infer 1 or both.Playground Link:
https://www.staging-typescript.org/play?ts=4.0.0-beta#code/PTAEDMFcDsGMBcCWB7apkDcCmAnANsgIYAmAUFHEqqNIRgBQAeAXKPfMqwM7w6LQBzADQQcyALYBGbr37CaWRvFbRI4gEa4AlKAC8APlAZkiYltbHTAbnIwEKNLQYs2HGX0EjwYqe7k6DIxMzC2CbCntqJyZWQmgAT3NQOPjQAG9SUCzQHCx4SBw0XkgsGwBfUlJo9mQvHxFoRQDDNNAynRBQAHdkHABrLiq6ehq6iWb0to6wHv7Bys7ILjlQeHiAByx5xXXe+FWNrFAAOToAFUO9TOyAHzYav09RCWlQHg95RqUVNU0cCcsZmuWTuIzcb1kT28L0eAgBwS0lVgqB4q1epwwF02elctWe4gaingE1aZSsoE6swGpGR0FR8AATKwMVijrpcWNxCS2uTOsRkFtoAByfZUyqkRbLQSgfjwXDgQiwI5dRDwAAW6Gw+CIxEGsvliqOAEloHKcAqlaz0sDcbDOa93nJCd8aL9tKFTDaHhCPvbYUlAaQKjSUftEK8TWaLVgrezRvjncS9C0eRSZr1qbTUYgmaBIwbLZc4xxOdyyWnQPzBSLuhnxZ0ABIAQQAwgBpVi8eIrDigIk4RX7dVHdaEAfiPK4GXQcDIfGrNXKjMrCJUNCYXAEEikNbYgCiSgHCAAYnY1wAeM59pRYaC60CnygOQzsq9E2-3kbgVj8cBTgAKEwpDoAD8oD-qAKhYFqNg7HsBzYgAypAo44gevCDo+kTQOeu5YMg4A0HQ+g2KQxBYLAeBjkcq4OERGAMvQ0CsMho4BmEQwMWCeLQgSCiMGW0yVgKXCmrW-ScYx8a8YJFZUvM4pAA
Related Issues:
The text was updated successfully, but these errors were encountered: