microsoft / microsoft/TypeScript

Type narrowing based on iterator `done` value?

オープン
#31,952 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

@rbuckton がすでに取り組んでいます。

2019年6月26日 から。

Needs Investigation
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。