microsoft / microsoft/TypeScript

Generator functions which have a return type of a union of Iterator/AsyncIterator have a yield type of never

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

まだ誰も着手していません。

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

説明

TypeScript Version: 3.7.2

Search terms: generator iterator asynciterator type union never

Code

example.ts

type NumberIterator = Iterator<number> | AsyncIterator<number>;
type NumberGenerator = Generator<number> | AsyncGenerator<number>;

function *numbers(): NumberIterator {
    yield 1;
}

A type alias which is a union of Iterator and AsyncIterator is used as the return type of a synchronous generator.

Expected behavior:

Code compiles.

Actual behavior:

example.ts(5,11): error TS2322: Type '1' is not assignable to type 'never'.

If you replace NumberIterator with NumberGenerator as the return type the function above, the code correctly type-checks. Something is wrong here.

Playground Link:

https://www.typescriptlang.org/play/index.html?target=99&ssl=5&ssc=2&pln=1&pc=1#code/C4TwDgpgBAcgrgWwEYQE4ElhoIbAPapQC8UmO+qAPAHaIqoB8UAPlAIIDOI1AxmargI06aBgG4AUBIBmcXsACWealABUtZGg4AKAJQAuWCIxYBFKAG8JUG1BAKIAGwAmUAIySAvkA

The playground link won’t work because the playground doesn’t support async iterators yet (https://github.com/microsoft/TypeScript-Website/issues/15).

Related Issues:
#30790 Strongly-typed iterator PR

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

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

はじめの一歩

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

調査の方向性

example.ts の TypeScript 3.7.2 の診断を再現し、NumberIterator と NumberGenerator の戻り値の型を比較します。イテレーターのコンテキストについて関連する issue #30790 を読み、その後、Iterator と AsyncIterator のユニオンを持つ同期ジェネレーターが yield-type-never エラーなしでコンパイルできることを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
45/100

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

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