question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Destructured union of tuples with non-uniform length results in diagnostic when narrowing

See original GitHub issue

Bug Report

🔎 Search Terms

tuple type union destructuring narrowing control flow analysis non-uniform length varying length

🕗 Version & Regression Information

  • This appeared in TS 4.6 when the feature was introduced.

⏯ Playground Link

Playground link with relevant code

💻 Code

declare const [a, b, c]: ["a", number] | ["b", number, boolean];
if (a === "a") {
    // If this line is commented, the diagnostic is not thrown
    c;
} else {
    c;
}

🙁 Actual behavior

When c is accessed in the branch for ["a", number], it throws a diagnostic on the original declaration for c since the control flow analysis tries to access narrowedType[2] which does not exist on ["a", number]

🙂 Expected behavior

There should be no diagnostic thrown and c should simply be undefined (like it already is) or the diagnostic should be thrown where c is accessed rather than defined.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
RyanCavanaughcommented, Jun 22, 2022

I don’t see how either proposed behavior is necessarily better than the current one. It’s very weird to get an error about “no element at position 2” on a bare identifier reference – if you were many lines down in the function, that would seem like a bug, or possibly extremely misleading if you wrote something like arr[c].

0reactions
Fireboltofdeathcommented, Jun 24, 2022

Tuples can’t have excess members. This is not the case with objects, so it can’t reasonably conclude that z is undefined but in the tuple case it is safe to.

playground

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript narrowing down within union of tuples
TypeScript's discriminating unions don't keep track of the dependency between a and b , so after you destructure the array, narrowing a ...
Read more >
Proceedings of the 60th Annual Meeting of the Association for ...
Proceedings of the 60th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers) · Smaranda Muresan, Preslav Nakov, Aline ...
Read more >
Untitled
Sinister souls beat the drum hard rar, Surface design association alberta? ... Olympic gymnastics uneven bars results, Financiamiento, Karlskronahem vd, ...
Read more >
IROS 2022 Program | Monday October 24, 2022
Design of a Reconfigurable Robot with Size-Adaptive Path Planner ... 3D Single-Object Tracking with Spatial-Temporal Data Association ...
Read more >
DeepDive: Declarative Knowledge Base Construction - PMC
We focus on a narrower question, with the axiom that the more rapidly ... Essentially, every tuple in the database or result of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found