Destructured union of tuples with non-uniform length results in diagnostic when narrowing
See original GitHub issueBug 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:
- Created a year ago
- Reactions:1
- Comments:8 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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]
.Tuples can’t have excess members. This is not the case with objects, so it can’t reasonably conclude that
z
isundefined
but in the tuple case it is safe to.playground