Co-dependency of variables destructured from union types is lost unless used in control-flow statements #55344
Labels
Help Wanted
You can do this
Possible Improvement
The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
🔎 Search Terms
destructuring union type tuple
Please expand this list or edit the title with a better-fitting description.
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play?ts=5.1.6#code/CYUwxgNghgTiAEYD2A7AzgF3gNyhAjAFzxxTCoQCe8A2gORR0A08DdAuvAD4khkXV6AI2asR7ANwAoAPQz4CxUoUA9APxSpydFhrZ8LbACZOAXhx580rakwWIR+Ob0GcJ+FDSJbGaXKXqsvIAkl4oSPAQqADmIDC8-ChUtABEUCksaSmcPKTkSYIpQhnwRdlSQA
💻 Code
🙁 Actual behavior
When destructuring a variable
readonly ["a", "a"] | readonly ["b", "b"]
into[v1, v2] = val
, the compiler is sophisticated enough to retain the dependency of the two when either is used in either anif
or even aswitch
statement:When put back together, however, that dependency is lost. For:
val1
andval2
should be identical, however,val2
isreadonly ["a" | "b", "a" | "b"]
. Note that this not only applies to tuples, but de- and restructured objects as well.See this real example of a legitimate use-case and how this affects the code.
🙂 Expected behavior
Just like with
the compiler should figure out that
v1
andv2
depend on each other when combining them with e.g.[v1, v2]
and create an according union type of tuples instead of a single tuple with union type members.The text was updated successfully, but these errors were encountered: