microsoft / microsoft/TypeScript

Tuple with optional element and exactOptionalPropertyTypes still returns undefined during iteration

Aperta
#54,302 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: flag: exactOptionalPropertyTypes Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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).

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la riproduzione collegata in TypeScript Playground e traccia il modo in cui il compilatore tipizza un ciclo for-of su un elemento di tupla opzionale con exactOptionalPropertyTypes abilitato. Individua l’area dei test di regressione del compilatore per l’iterazione delle tuple, quindi aggiungi la copertura per i casi Foo e Bar mostrati; il lavoro è completato quando l’elemento del ciclo di Foo viene trattato come Item, mentre l’accesso indicizzato potenzialmente assente continua a essere rifiutato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.