microsoft / microsoft/TypeScript
Tuple with optional element and exactOptionalPropertyTypes still returns undefined during iteration
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
When iterating over a variable with a tuple type with optional element [T?], the type of the item is still T | undefined even when exactOptionalPropertyTypes: true (i.e. creating a variable like let foo: [string?] = [undefined] errors).
🔎 Search Terms
tuple, optional tuple element, exactOptionalPropertyTypes
🕗 Version & Regression Information
This is the behavior in every relevant version I tried (4.4.4 to 5.1-beta), and I reviewed the FAQ for entries about tuples
⏯ Playground Link
Playground link with relevant code
💻 Code
type Item = {
value: string;
};
type Foo = [Item?];
let foo1: Foo = []; // no error, good, there can be 0 or 1 item
let foo2: Foo = [undefined]; // error, good, because of `exactOptionalPropertyTypes: true`; if I turn this option off, the error goes away
let foo3: Foo = [{value: '1'}]; // no error, good, obviously
console.log('1');
for (let item of foo1) {
console.log('2');
console.log(item.value); // error, but why? `'2'` isn't logged during runtime
}
console.log('3');
console.log(foo1[0].value); // error, good, because there may be no item
console.log('4'); // this isn't logged because of runtime error on previous line
type Bar = [Item];
let bar: Bar = [{value: '1'}];
for (let item of bar) {
console.log(item.value); // no error, good
}
🙁 Actual behavior
Error on line 15 (console.log(item.value);) because item has type Item | undefined.
🙂 Expected behavior
I would expect no error on line 15 and the type of item to be Item. Is this maybe somehow related to #51643? Or is my expectation incorrect and there's some possible runtime behavior that I'm not taking into account? But I think that e.g. noUncheckedIndexedAccess doesn't apply in this situation (and toggling it off and on didn't change anything anyway). I also tried readonly [Item?], although it too doesn't affect anything (as I think it shouldn't).
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 der verlinkten TypeScript Playground-Reproduktion und verfolgen Sie, wie der Compiler eine for-of-Schleife über ein optionales Tupel-Element mit aktiviertem exactOptionalPropertyTypes typisiert. Suchen Sie den Bereich der Regressionstests des Compilers für Tupel-Iteration und fügen Sie Abdeckung für die gezeigten Foo- und Bar-Fälle hinzu; abgeschlossen ist die Aufgabe, wenn das Foo-Schleifenelement als Item behandelt wird, während der potenziell fehlende indizierte Zugriff weiterhin abgelehnt wird.
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
- 45/100