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
Expected behavior:
Expected to compile without any errors, with the type of self inferred as { val: number, func(): number }. This works as expected from v3.1 -> 3.4.3
Actual behavior:
As of 3.4.4 this no longer works. Looks like self is being inferred as {}. TypeScript emits the following error at return self.val:
9:17 - error TS2339: Property 'val' does not exist on type '{}'.
I tested this with TypeScript@3.5.0-dev.20190507 and it looks like self is being inferred as unknown. It emits the following error:
Anders Hejlsberg (@ahejlsberg) is this an effect of instantiating the func method's return type too soon and not recognizing the non-inferrable type required to calculate it in the first pass?
This is working as intended. In the call to wrap, the self parameter is contextually typed by Self. Since there are no other arguments and no contextual return type to make inferences from, we end up fixing Self to type unknown. And once we have committed to a type for Self we can't make any further inferences and therefore infer nothing from the return type of the lambda.
In half-way worked before because of a bug that was fixed in #30856. You can see the inconsistency with some quick info hovering. For example, inside func, statement completion doesn't work on self and hovering shows type { val: number } which certainly isn't correct.
Activity
weswigham commentedon May 8, 2019
Anders Hejlsberg (@ahejlsberg) is this an effect of instantiating the
funcmethod's return type too soon and not recognizing the non-inferrable type required to calculate it in the first pass?weswigham commentedon May 8, 2019
Actually,
funcis totally annotated, I see no reason why we would fixselftoo soon. o.Oahejlsberg commentedon Jun 12, 2019
This is working as intended. In the call to
wrap, theselfparameter is contextually typed bySelf. Since there are no other arguments and no contextual return type to make inferences from, we end up fixingSelfto typeunknown. And once we have committed to a type forSelfwe can't make any further inferences and therefore infer nothing from the return type of the lambda.In half-way worked before because of a bug that was fixed in #30856. You can see the inconsistency with some quick info hovering. For example, inside
func, statement completion doesn't work onselfand hovering shows type{ val: number }which certainly isn't correct.zcregan commentedon Jun 12, 2019
If we expand on this example a bit, the behaviour still seems a little strange to me. If the
wrapfunction were to return an instance ofSelf:Then we get two different return types depending on if we reference
selfwithin the lambda:From your explanation I vaguely understand why this is the case, but is that behaviour desired?
zcregan commentedon Jun 12, 2019
I was hoping for the following behaviour:
ahejlsberg commentedon Jun 12, 2019
Did you consider this instead:
Everything is inferred correctly and no need to double allocate the object.