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
TS does not propagate type guards accross function captures.
Rightly so because we don't know when the lambda will actually be called. It can be called much later and at that point name might be null again. E.g. if right after bar(...) we have name = null.
I suggest that because name is declared with const, it can't change. So if at the point of capture it has a not null type guard, then it is correct to propagate this information inside the lambda.
👉 The second example can compile without error.
Currently we only extend const type guards to nested function expressions and arrow functions, but we ought to also include methods in object literals and class expressions. Until we have a fix, the easy workaround for object literals is to just use regular properties and function expressions. For example, this works:
Activity
RyanCavanaugh commentedon Aug 23, 2016
This is already not an error in
master.jods4 commentedon Aug 23, 2016
Sorry it seems my short repro was not good enough.
After testing the code above works even on 2.0.0, but try this instead:
Not sure what the difference is here... Method in object literals are considered differently than plain lambdas?
RyanCavanaugh commentedon Aug 23, 2016
ahejlsberg commentedon Aug 23, 2016
Currently we only extend const type guards to nested function expressions and arrow functions, but we ought to also include methods in object literals and class expressions. Until we have a fix, the easy workaround for object literals is to just use regular properties and function expressions. For example, this works:
Narrowed consts flow through object literal or class expression method