I am currently facing an issue with Typescript when trying to compile a generator-loop that works perfectly in a modern browser. The code snippet in question is:
/** Should print "x= 1 y= 2" **/
function* gen() { yield [1, 2] }
for (const [x, y] of gen()) { console.log("x=", x, "y=", y) }
Upon running this code through Typescript, it fails to execute as expected. By inputting the code into the Typescript Playground, I was able to observe that the for-of
loop gets converted to an array iteration loop, whereas the generator function returns an object.
This seems like an issue with Typescript. Is anyone else experiencing this problem? I couldn't find any information related to this on https://github.com/Microsoft/TypeScript.
What would be the best workaround for this situation? Would using Array.from
on the generator function be the most appropriate solution?