facebook / facebook/flow

Endless iterator

Aperta
#3,085 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
iterators
Lingua principale
Rust
Stelle
22.3k
Fork
1.9k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.