Skip to content

Type narrowing for Tuple-Elements #12849

Closed

Description

TypeScript Version: nightly (2.2.0-dev.20161210)

Code

function test([a, b]: [number, number] | [string, string]) {
  if (typeof a === 'number') {
    b++
  }
}

Expected behavior:
b to be narrowed to number and the resulting code to throw no compiler errors

Actual behavior:
b has type number | string and thus the code throws a compiler error complaining that b++ can only apply to numbers

Activity

  1. mhegazy commented on May 5, 2017

    @mhegazy
    Contributor

    Same underlying issue as #14957

  2. christyharagan commented on May 6, 2017

    @christyharagan
    Author

    Should I close this in favour of #14957 ?

  3. mhegazy commented on May 8, 2017

    @mhegazy
    Contributor

    I would leave them both open. though both would be addressed by the same change, ppl tend to think of them as two separate scenarios. this way we have a landing point for users searching for similar issues.

  4. added
    Design LimitationConstraints of the existing architecture prevent this from being fixed
    and removed
    SuggestionAn idea for TypeScript
    on Jul 22, 2020
  5. RyanCavanaugh commented on Jul 22, 2020

    @RyanCavanaugh
    Member

    This would work if it weren't for the destructuring. Our CFA algorithm isn't able to correlate values from different places like this; this comes up from time to time but doesn't seem to merit the required work at this time.

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