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
interfaceA{foo?: {x: number};bar?: {y: number};}functionf(a: A){if(a.foo&&a.bar){constxx=a.foo.x;constyy=a.bar.y;// No errorconst{foo: { x },bar: { y }}=a;// Error}}
Expected behavior:
No errors.
Actual behavior:
a.ts(9,24): error TS2459: Type '{ x: number; } | undefined' has no property 'x' and no string index signature.
a.ts(9,36): error TS2459: Type '{ y: number; } | undefined' has no property 'y' and no string index signature.
This is a design limitation. The narrowing expression only narrows a.foo and not a itself; so when the destructring happens you still have the undefined. I will try to look up the original issue later for you.
Activity
mhegazy commentedon Mar 13, 2017
This is a design limitation. The narrowing expression only narrows a.foo and not a itself; so when the destructring happens you still have the undefined. I will try to look up the original issue later for you.