[Transforms] Minimal authoring support for down-level generator functions #10106
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Our current implementation of down-level support for generator functions in the transforms branch only supports generator functions created as part of the async/await transformation. This pull request relaxes the restrictions we added to the checker to allow developers to author generator functions that can be used in down-level host environments such as ES5/ES3. As a result, developers are able to use powerful features of generator functions in ES5/ES3 for scenarios such as coroutines in the same way TypeScript leverages this support for async functions.
The following features of ES6 generators are supported down-level:
Iterator<T>(but not anIterableIterator<T>).yieldexpression.yield*expression when its value is anIterator<T>.The following features of ES6 generators are not supported down-level:
for..of, spread, or destructuring, as it is not an array.Iterator<T>) to theyield*expression.The following features differ between ES6 and down-level generators when type-checking:
Iterator<T>rather thanIterableIterator<T>.Here are some examples of the above features and restrictions:
The reason we've chosen to only support iterators for
yield*is to allow for generator composition.Please note that it is possible to partially work around the above restrictions in your code by implementing your own conversion functions, for example:
We do not perform the above operations by default as they do not have the same runtime semantics as ES6 (i.e.
toArrayeagerly consumes the iterator), and we cannot guarantee the availability of a globalSymbol.iteratordown-level.