microsoft / microsoft/TypeScript

Type narrowing based on iterator `done` value?

Aperta
#31,952 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@rbuckton ci sta già lavorando.

Dal 26/6/2019.

Needs Investigation
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

TypeScript Version: 3.4.0-dev.201xxxxx

Search Terms:

Code

interface MyIterator<T> {
    next():
      | {
          done: false;
          value: T;
        }
      | {
          done: true;
          value: undefined;
        };
    [Symbol.iterator](): MyIterator<T>;
}

interface Item { name: string }

function m(iter: MyIterator<Item>) {
    for (const it of iter) {
        console.log(it.name);           // Error here complaining about 'it': Object is possibly 'undefined'.ts(2532)
    }
}

Expected behavior:

Actual behavior:

Playground Link:
https://typescript-play.js.org/?downlevelIteration=true#code/JYOwLgpgTgZghgYwgAgLIE8CSkpzAeygB4AVAPmQG8AoZO5ECADzAAoBKALlvroB8qPXrwAm+Rp2TwANgGcIAbiHC6ANzjSArhEkklKugF9l-QQfpiJyMFG37zydVp3JNIERBigII+yuO8fsgA2gDK6AC2AEb40gB0wDh4hAC6HJIY2NDJxORKxtSgOPBIyFkRVAxwES6yNqAA5sgFMG4IYMDiyBGsidAZWEkExOVk7Gb0MITIrAjidciJyPgwizjjNOZzILKxEHHS+A29YHEg1RDsCg50APS3yACiUFDTABbQKHMRAA7ScKBGsg4DFNGBkAByRIQyQAeSiACsIO1FrJkD98LJZMAotJ0JC3B4vIwRBC4mBZKwAEwAVgAzFT2NRlMZDEA

Related Issues:
https://github.com/microsoft/TypeScript/issues/11450 seems related. but here even with --strictNullChecks same error is reported here.

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.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.