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
{{ message }}
Repository navigation
Overload Regression in TS2.7 -- the first matching overload is not selected #22471
typeProp<T>={(): T}declarefunctiontest<T>(a: Prop<T>): Tdeclarefunctiontest(a: Prop<any>): {}// toggle this linevara=test(Array)a.push
Expected behavior:
Code compiles. Since the first overloading should always match.
Actual behavior:
Code doesn't compile. TSC chooses the second overloading instead of the first one.
Toggling off the second overload signature can make code compile but should not.
Note: changing Array to String belies the problem.
changed the title [-]Overload Regression in TS2.7 -- the first hit overload is not selected[/-][+]Overload Regression in TS2.7 -- the first matching overload is not selected[/+]on Mar 11, 2018
Reduced further (thanks for getting it so small to begin with!):
typeProp<T>={(): T}declarefunctiontest<T>(a: Prop<T>): T;declarefunctiontest(a: Prop<any>): {};declarefunctionfn(): any[];declarefunctionfn<T>(item?: T): T[];vara=test(fn);// Check inferred type of above without introducing contextual return typevara: any[];
Confirmed broken between 2.6 and 2.7. Anders Hejlsberg (@ahejlsberg) this is similar to one we look at last week but there's less weird inference-from-return-type in play here. Thoughts?
This is caused by #19424 which indeed was a breaking change. We are now more precise when inferring from generic signatures in the sense that we erase type parameters to their constraints instead of blindly erasing them to any. In both of the examples above we're inferring from generic signatures (because the last, and therefore supposedly most general, signatures in Array and fn are generic), and in both cases we end up erasing the signature type parameter to {} instead of any.
Activity
[-]Overload Regression in TS2.7[/-][+]Overload Regression in TS2.7 -- the first hit overload is not selected[/+][-]Overload Regression in TS2.7 -- the first hit overload is not selected[/-][+]Overload Regression in TS2.7 -- the first matching overload is not selected[/+]RyanCavanaugh commentedon Mar 13, 2018
Reduced further (thanks for getting it so small to begin with!):
Confirmed broken between 2.6 and 2.7. Anders Hejlsberg (@ahejlsberg) this is similar to one we look at last week but there's less weird inference-from-return-type in play here. Thoughts?
ahejlsberg commentedon Apr 2, 2018
This is caused by #19424 which indeed was a breaking change. We are now more precise when inferring from generic signatures in the sense that we erase type parameters to their constraints instead of blindly erasing them to
any. In both of the examples above we're inferring from generic signatures (because the last, and therefore supposedly most general, signatures inArrayandfnare generic), and in both cases we end up erasing the signature type parameter to{}instead ofany.Ryan Cavanaugh (@RyanCavanaugh) There's no inference from return type in play here.