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
Type narrowing context is lost within closuresΒ #46118
typeMyUnion={type: 'left',propertyExistsOnLeft: string}|{type: 'right',propertyExistsOnRight: string}letx={}asMyUnion;switch(x.type){case'left':
consty=x.propertyExistsOnLeft;//all good here ...console.log(()=>x.propertyExistsOnLeft)//showing error "Property 'propertyExistsOnLeft' does not exist on type 'MyUnion'"break;}
π Actual behavior
Type narrowing is working as expected when following control flow, however when the variable is used inside a closures scope (arrow functions or normal functions) it reverts back to it's declared type losing all narrowing inferred from the containing scope.
π Expected behavior
Type narrowing should continue to work inside the context of closures.
The exact same issue you describe comes up again and again and again. I probably have seen at least 50 issues already. It's all due to the difficulties described in #9998.
Activity
[-]Type narrowing lost within closures[/-][+]Type narrowing context is lost within closures[/+]MartinJohns commentedon Sep 28, 2021
And another duplicate of #9998.
ramyhhh commentedon Sep 28, 2021
Yeah, that issue looks like a master issue collecting all control flow analysis issues :)
MartinJohns commentedon Sep 28, 2021
The exact same issue you describe comes up again and again and again. I probably have seen at least 50 issues already. It's all due to the difficulties described in #9998.
fatcerberus commentedon Sep 28, 2021
More specifically, control flow analysis issues across function boundaries. Which is not an easy problem to solve.
typescript-bot commentedon Oct 3, 2021
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.