Closed
Description
Search Terms
- generator
- yield
Suggestion
- Correct return type on
yield
operator - Correct return type on
yield*
operator - Correct type for generator function return
Use Cases
better type safety
Example
now:
function* f() { // :IterableIterator<number>
const x = yield 0; // x is any
const y = yield* [2, 3, 4]; // y is any
}
desired:
function* f1(): TypedIterableIterator<number, number> {
const x = yield 0; // x is number
const y = yield* [2, 3, 4]; // y is number
yield x + y;
}
function* f2() { // :IterableIterator<number, number>
const x: number = yield 0;
const y: number = yield* [2, 3, 4];
yield x + y;
}
function* f3(): TypedIterableIterator<number> {
const x = yield 0; // x is number
const y = yield* [2, 3, 4]; // y is number
yield x + y;
}
interface TypedIterableIterator<T, N = any> {
next(value: N): T;
}
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript / JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. new expression-level syntax)