question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generator helper should include [Symbol.toStringTag]

See original GitHub issue

TypeScript 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:

#19006

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
DanielRosenwassercommented, Jan 15, 2020

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.

0reactions
sirrealcommented, Jan 23, 2020

In https://github.com/WordPress/gutenberg/pull/19666 we changed the check in @wordpress/redux-routine to typeof 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 the Symbol.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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Symbol.toStringTag - JavaScript - MDN Web Docs
The Symbol.toStringTag well-known symbol is a string valued property that is used in the creation of the default string description of an ...
Read more >
What does the GeneratorFunction interface stands for in ...
toStringTag ]' is missing in type '() => Generator<never, void, unknown>' but required in type 'GeneratorFunction'.ts(2741) lib.es2015.symbol.
Read more >
String Literal types on es6 interfaces for [Symbol.toStringTag]?
The goal of this property is to provide a distinct value, using a string literal type for the interface is overly specific. Actual...
Read more >
ECMAScript proposal: iterator helpers - 2ality
However, we'd also like to use generators to implement iterables. That's why generator objects have a method [Symbol.iterator]() that ...
Read more >
How to use toStringTag function in SymbolConstructor - Tabnine
newChildrenIterable[Symbol.toStringTag] === 'Generator') { if (!didWarnAboutGenerators) { error('Using Generators as children is unsupported and will likely ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found