Generator helper should include [Symbol.toStringTag]
See original GitHub issueTypeScript Version: 3.8.0-dev.20191223
and 3.7.3
Search Terms:
Generator, toStringTag, transformer, helper, tslib
Code
The following code will error in when using compilerOptions.target = es5
, but works correctly when compiled with target = es2015
or greater.
// index.ts
function* f() {}
if (f()[Symbol.toStringTag] !== 'Generator') {
throw new Error("Bad generator!")
}
console.log('👍 Works well!');
tsc --target es5 --lib dom,es5,es2015 ./index.ts && node index.js
# Error: Bad generator!
tsc --target es2015 ./index.ts && node index.js
# 👍Works well!
Expected behavior:
The property should be set as expected via the helpers.
Generators should include the Symbol.toStringTag
property "Generator"
according to spec.
Some libraries check for a generator by inspecting this property. For example, @wordpress/redux-routine
checks for this property and does not correctly handle generators produced by the helper.
regenerator-runtime
is a widely used generator helper which does set this property.
Actual behavior:
Symbol.toStringTag
is not included on the generator.
Playground Link:
Broken with target=es5 Working with target=es2015
Related Issues:
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:8 (5 by maintainers)
Yeah, I think that’s probably a more appropriate check, though you’d want to name the function something like
isIterable()
if you did that. Double check with @aduth though of course, since I don’t want to assume the wrong intent as an outsider.In https://github.com/WordPress/gutenberg/pull/19666 we changed the check in
@wordpress/redux-routine
totypeof object[ Symbol.iterator ] === 'function' && typeof object.next === 'function'
. This is effectively testing the generator interface.@wordpress/redux-routine
expects to operate on action objects. While an array of objects might make sense, one example that clearly doesn’t make sense here but satisfies theSymbol.iterator
check is strings, i.e.typeof "foo"[Symbol.iterator] === 'function'
. The library doesn’t want to treat the characters of a string as if they were yielded actions.Thanks for the feedback everyone. With the change in
@wordpress/redux-routine
, my needs are satisfied. I’ll leave this to maintainers to decide whether there’s anything to more to consider here or if you’d like to close.