microsoft / microsoft/TypeScript

Use any instead of unknown for AsyncGenerator optional .next parameter

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

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

2019年9月17日 から。

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

説明

TypeScript Version: 3.6.2

Search Terms:
async generator, AsyncGenerator, optional .next

Code

The following causes a type error:

async function* sequence(iterable: AsyncIterable<number>): AsyncGenerator<number> {
  yield 12;
  try {
    // Type error: Cannot delegate iteration to value because the 'next' method of its iterator expects type 'undefined', but the containing generator will always send 'unknown'.
    yield* iterable;
  } finally {
    console.log('Cleanup!');
  }
}

Expected behavior:

I would've expected it to be a non-type error.

Problem:

The primary annoyance with this is that I'd like to use AsyncGenerator<T> just for specifying that .return() can be used without non-null assertions (.return!()) for early cleanup but AsyncGenerator<T> results in AsyncGenerator<T, any, unknown> so AsyncIterable<T> can't be delegated to.

Proposed solution:

Change interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> to interface AsyncGenerator<T = unknown, TReturn = any, TNext = any.

This won't break anything as any is assignable to anything, and I doubt it'll be problematic as those using AsyncGenerator<T> (or AsyncGenerator<T, S>) presumably do not care about the .next parameter.

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

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

はじめの一歩

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

評価

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

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

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