facebook / facebook/flow

Endless iterator

Đang mở
#3,085 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
iterators
Ngôn ngữ chính
Rust
Star
22.3k
Fork
1.9k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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?**

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.