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
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.
TypeScript Version: 3.4.0-dev.201xxxxx
Search Terms: Narrowing, "Tagged Unions"
Code
Expected behavior:
I would expect to be able to pull the
kind
property from theShape
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.
Playground Link: TS Playground
Related Issues: #18758
The text was updated successfully, but these errors were encountered: