facebook / facebook/flow

Endless iterator

オープン
#3,085 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る
iterators
主要言語
Rust
スター
22.3k
フォーク
1.9k
PR マージ指標
30日以内にマージされた PR はありません

説明

Let's suppose we have following code:

```js
function* gen(): Iterator {
while (true)
yield 2
}

const generator: Iterator = gen()
const generatorResult: IteratorResult = generator.next()
// const two:number = generatorResult.value // typechecker forbids this
if (!generatorResult.done) {
const two:number = generatorResult.value
}
```

`generator` will never return. It will always yield. Thus we can say that it type is no only `Iterator` but it can be more precisely defined as

```js
interface EndlessIterator {
next(): {done: false, value: T};
}
const generator: EndlessIterator = gen()
```

This allows to directly obtain next value of the generator by `generator.next().value` instead of unnecessary check of `done` property.

**Does Flow allow to somehow change return type of generator function to something like `EndlessIterator` to avoid unnecessary `done` property checks?**

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

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

評価

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

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

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