Skip to content

Incorrect narrowing in control flow analysis when destructuring union of tuples of different lengths #55661

Closed

Description

@cebamps

🔎 Search Terms

Tuple union, Tuple destructuring assignment, Array destructuring assignment, control flow analysis, non-uniform length, varying length, never

🕗 Version & Regression Information

This behaviour appears to exist since the introduction of control flow analysis on destructured data in TS 4.6, and still appears in nightly.

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.2.2#code/GYVwdgxgLglg9mABFOcByBDATluB3ACgG0APAGkQE8BdALkSIEYLHrEAfB5xAJjc6bUAlIgDeAKERTEMYIgKVEAXhWJwAEwCmwGGE3qRE6ccQQEAZyiJzACzggANurRwoAIU0B5ANb09AN00sZUQSAG5JaQBfcSigA

💻 Code

function tooNarrow([x, y]: [1, 1] | [1, 2] | [1]) {
    if (y === undefined) {
        const shouldNotBeOk: never = x;
    }
}

🙁 Actual behavior

Outside the conditional, x has type 1 and y has type 1 | 2 | undefined, which is what I expect.

Inside the conditional, the type of x is narrowed down to never, which is not expected. The assignment type-checks.

Interestingly, we seem to need three tuples to trigger the issue. Using [1, 1] | [1] or [1, 2] | [1] results in correct behaviour.

🙂 Expected behavior

x should have type 1 inside the conditional. The assignment should then not type-check.

Additional information about the issue

Possibly relates to:

Activity

  1. added
    BugA bug in TypeScript
    Effort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".
    on Sep 11, 2023
  2. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions