Bug Report
🔎 Search Terms
typescript union typed narrowing lost in closures
type narrowing closures
🕗 Version & Regression Information
Typescript versions 4.4.3 and still exists on Nightly 4.5.0-dev
⏯ Playground Link
Playground link with relevant code
💻 Code
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.
Bug Report
🔎 Search Terms
typescript union typed narrowing lost in closures
type narrowing closures
🕗 Version & Regression Information
Typescript versions 4.4.3 and still exists on Nightly 4.5.0-dev
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 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.