Skip to content

Bug in destructuring assignment, losing type analysis information  #12263

Closed

Description

TypeScript version: 2.0.8

export type State = {
    value: string,
    mode: Selecting | Selected,
}

type Selected = {
    type: 'selected',
    valid: boolean
}

type Selecting = {
    type: 'selecting',
    options: { type: 'loading', handle: number }
        | { type: 'loaded', selection: undefined | number, items: string[] }
}

function f(state: State) {
    if (state.mode.type === 'selecting' && state.mode.options.type === 'loaded') {
        const {options} = state.mode;
        const options2 = state.mode.options;
        options2.items;        
        options.items;
    }
}

Expected
inside f, options and options2 should have the same type

Actual
options loses the narrowed type information

Activity

  1. ahejlsberg commented on Nov 15, 2016

    @ahejlsberg
    Member

    Duplicate of #10065 and #10846.

  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

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions