Open
Description
Suggestion
π Search Terms
yield ordered return types
β Viability 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. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
β Suggestion
Make types more definite for each yielded return value in a generator function
π Motivating Example
Right now, if I do this:
function* work() {
yield 1;
yield 'test';
return [123];
}
Right now this gives:
const g = work();
g.next().value // string | number | number[]
g.next().value // string | number | number[]
g.next().value // string | number | number[]
But since there's no logic/branching/etc, it would be more helpful to me to have:
const g = work();
g.next().value // number
g.next().value // string
g.next().value // number[]
π» Use Cases
Sometimes I want to use a function generator to represent a series of steps that I want to examine (whether at runtime or during testing, etc) before deciding whether to continue. In other words, I want to break up a function into a series of "pauseable" steps.