Skip to content

Bad null error in destructuring #14630

Closed
Closed

Description

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

Activity

  1. mhegazy commented on Mar 13, 2017

    @mhegazy
    Contributor

    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.

  2. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions