microsoft / microsoft/TypeScript
Co-dependency of variables destructured from union types is lost unless used in control-flow statements
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 Search Terms
destructuring union type tuple
Please expand this list or edit the title with a better-fitting description.
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about n/a
⏯ Playground Link
💻 Code
declare const val1: readonly ['a', 'a'] | readonly ['b', 'b'];
// ^?
const [v1, v2] = val1;
const val2 = [v1, v2] as const;
// ^?
// Is no longer readonly ["a", "a"] | readonly ["b", "b"]
🙁 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 an if or even a switch statement:
if(v1 === 'a') {
v2;
// ^?
// TS is smart enough to know that this is "a"
}
When put back together, however, that dependency is lost. For:
declare const val1: readonly ['a', 'a'] | readonly ['b', 'b'];
const [v1, v2] = val1;
const val2 = [v1, v2] as const;
val1 and val2 should be identical, however, val2 is readonly ["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
if(v1 === 'a') {
v2;
// ^?
// TS is smart enough to know that this is "a"
}
the compiler should figure out that v1 and v2 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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la riproduzione collegata in TypeScript Playground e confronta i tipi inferiti per val1, le variabili destrutturate e val2. Traccia la gestione esistente del controllo di flusso del compilatore per gli esempi di if e switch, quindi verifica che la ricostruzione dei valori preservi il tipo tupla-unione correlato, incluso il caso dell’oggetto referenziato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100