Typed generators
A generator function yields a sequence that callers consume with for-of.
TypeScript · Interface
IterableIterator represents an object that is both iterable and an iterator. It can participate in for-of and similar iteration constructs while producing values through the iterator protocol.
IterableIterator describes an object that combines the iterable and iterator protocols. It can provide its own iterator through Symbol.iterator and advance through values with next(), allowing it to work with constructs such as for-of.
interface IterableIterator<T> extends Iterator<T> {
[Symbol.iterator](): IterableIterator<T>;
}An IterableIterator is an object usable as both an iterable and an iterator; its next method produces iterator results.
function* values(): IterableIterator<number> {
yield 1;
yield 2;
}
for (const value of values()) {
console.log(value);
}The generator returns an object that can both produce iterator results and be consumed directly by a for-of loop.
A generator function yields a sequence that callers consume with for-of.
An API exposes a lazy sequence without allocating an array of all values.
A custom object supports both direct iteration and explicit next() calls.
IterableIteratorGeneratorIteratorResultSymbol.iterator