microsoft / microsoft/TypeScript
Spread operator mixed with destructuring assignment resulting in unsoundness
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
Bug Report
This might have been covered before but I am not sure how to best search for it, apologies if it is a duplicate - when destructuring assignment is used with a spread operator (see example below) TS seems to ignore the possibility that the variable could be assigned undefined.
🔎 Search Terms
spread, destructure, destructuring
🕗 Version & Regression Information
- This is the behavior in every version I tried (since 4.1.5 which is the oldest version on the playground with
noUncheckedIndexedAccessavailable)
⏯ Playground Link
Playground link with relevant code
💻 Code
const [a] = [...(false ? (["a"] as const) : [])];
// ^?
const b = false ? (["a"] as const) : [];
// ^?
const c = [...b];
// ^?
const [d] = c;
// ^?
const [a_prime] = [...(false ? (["a"] as const) : [] as const)];
// ^?
🙁 Actual behavior
a is typed as "a", while d and a_prime are typed as "a" | undefined". Note this only happens when noUncheckedIndexedAccess is enabled, otherwise all of them are typed as "a".
Note that d is the unrolled version of a which makes it more surprising that TS is typing them differently.
🙂 Expected behavior
They should all be typed "a" | undefined.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginnen Sie mit dem verknüpften TypeScript Playground-Beispiel und vergleichen Sie die abgeleiteten Typen von a, d und a_prime, wenn noUncheckedIndexedAccess aktiviert ist. Verfolgen Sie die Verarbeitung von Array-Destrukturierung und Spread-Ausdrücken durch den Compiler; abgeschlossen ist die Aufgabe, wenn alle drei Fälle konsistent "a" | undefined ableiten und eine Regressionstestabdeckung für das gemeldete Beispiel hinzugefügt wurde.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100