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
Issue narrowing tagged unions with temporary const #31037
interfaceSquare{kind: 'square';size: number;}interfaceCircle{kind: 'circle';radius: number;}typeShape=Square|Circle;// works as expected (s.kind) does the correct narrowingfunctionareaWorking(s: Shape){switch(s.kind){case'square': returns.size*s.size;case'circle': returnMath.PI*s.radius**2;}}// When saving s.kind to a const variable, narrowing does not workfunctionarea(s: Shape){const{kind}=s;// same as const kind = s.kind;switch(kind){case'square': returns.size*s.size;// Property 'size' does not exist on type [Shape|Circle].case'circle': returnMath.PI*s.radius**2;// Property 'radius' does not exist on type [Shape|Square].}}
Expected behavior:
I would expect to be able to pull the kind property from the Shape object into a const and the narrowing to work as expected.
Actual behavior:
Narrowing does not work, I need have the switch() go off of the variable passed in.
Activity
[-]Issue narrowing tagged unions with intermediary const[/-][+]Issue narrowing tagged unions with temporary const[/+]jack-williams commentedon Apr 19, 2019
Narrowing isn’t tracked across variables; the issue tracking this is here: #12184. You’ll find related/duplicate issues linked to that one.
kyleshay commentedon Apr 19, 2019
Thanks!