Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad null error in destructuring #14630

Closed
ghost opened this issue Mar 13, 2017 · 1 comment
Closed

Bad null error in destructuring #14630

ghost opened this issue Mar 13, 2017 · 1 comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@ghost
Copy link

ghost commented Mar 13, 2017

TypeScript Version: nightly (2.3.0-dev.20170313)

Code

interface A {
    foo?: { x: number };
    bar?: { y: number };
}

function f(a: A) {
    if (a.foo && a.bar) {
        const xx = a.foo.x; const yy = a.bar.y; // No error
        const { 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.

May be related: #12620, #6784

@mhegazy
Copy link
Contributor

mhegazy commented 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.

@mhegazy mhegazy added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Mar 13, 2017
@mhegazy mhegazy closed this as completed Apr 21, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

1 participant