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
Over-eager inference with ThisType & homomorphic mapped types #18805
Here, you'll get a problem on bar and on the x in this.x, you'll get:
'bar' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.
Property 'x' does not exist on type '{ bar(): any; }'.
The problem surfaces as soon as any value on this is used in producing the return value of a computed method.
I forget exactly why this occurs, but it has to do with circularities in trying to draw inferences to C which is wrapped in a homomorphic mapped type. Anders Hejlsberg (@ahejlsberg) can fill me in here.
The solution for users is to add a return type annotation to bar which is not obvious, but it prevents type argument inference from type-checking the internals of the method, and avoids the circularity problem. Ideally, users should not have to think about this.
I just spent a few hours figuring out why inference was failing sometimes for one of my generic functions, finally narrowing it down to whether or not this.someProp was referenced in a return expression of a method inside an object. I suspected something strange going on with ThisType and that lead me to this issue. Thanks Daniel Rosenwasser (@DanielRosenwasser) for the workaround (adding a return annotation to the method).
I agree that it is not at all obvious. It's very surprising to see infered types totally change depending what a method returns, with no obvious causal link for the changed inference.
Activity
yortus commentedon Dec 18, 2017
I just spent a few hours figuring out why inference was failing sometimes for one of my generic functions, finally narrowing it down to whether or not
this.somePropwas referenced in a return expression of a method inside an object. I suspected something strange going on withThisTypeand that lead me to this issue. Thanks Daniel Rosenwasser (@DanielRosenwasser) for the workaround (adding a return annotation to the method).I agree that it is not at all obvious. It's very surprising to see infered types totally change depending what a method returns, with no obvious causal link for the changed inference.
RyanCavanaugh commentedon Jun 25, 2021
This works now