facebook / facebook/flow

Endless iterator

未關閉
#3,085 1 則留言 0 個 reaction 已指派 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 摘要。