Uh oh!
There was an error while loading. Please reload this page.
Use global Iterator.prototype for down-level generators - #59514
Conversation
Ron Buckton (rbuckton)
commented
Aug 2, 2024
TypeScript Bot (@typescript-bot): pack this |
Hey Ron Buckton (@rbuckton), I've packed this into an installable tgz. You can install it for testing by referencing it in your and then running There is also a playground for this build and an npm module you can use via |
Jake Bailey (jakebailey)
left a comment
There was a problem hiding this comment.
This needs a tslib update too, right?
| var __generator = (this && this.__generator) || function (thisArg, body) { | ||
| var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
| return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
| var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); |
There was a problem hiding this comment.
I assume it's not a problem if someone redefines the global Iterator, given we don't care if someone does that for other global symbols?
Ron Buckton (rbuckton)
commented
Aug 8, 2024
Correct. The tslib PR is microsoft/tslib#267 |
**Related issue:** - Closes#10365 - microsoft/tslib#186 - microsoft/TypeScript#59514
If you are compiling generators with
--target ES5 --lib esnextthe types of those generators will also include the iterator helpers methods likemap,filter, etc., though our down-level generator emit will not have those methods even when running with newer editions of V8 that do support them:Even when not running in latest V8, iterator helpers can be polyfilled, e.g.:
To better support both scenarios, this makes a small change to our
__generatorhelper to use the globalIterator.prototypeobject as the prototype for down-level generators, if it is present.This also does the same for our
__asyncGeneratorhelper when a globalAsyncIterator.prototypeobject is present, even though async iterator helpers are still at Stage 2. Support forAsyncIterator.prototypeseems harmless enough to add at this time but can be postponed to a later PR if necessary.Please note that this will also require the same change in
tslib.Fixes#59513