Skip to content

tsc transpile js to js converts Set iteration loops to invalid numeric key indexing #62816

@sparr

Description

@sparr

🔎 Search Terms

tsc set iteration loop indexing transpiler transpiling typescript javascript allowJS

🕗 Version & Regression Information

  • This is the behavior in every version I tried (one each of 1.x, 2.x, 3.x, 4.x, 5.x), and I reviewed all of the FAQ common "bugs" entries.

⏯ Playground Link

No response

💻 Code

const foo = new Set();
foo.add("bar");
console.log(foo.size);
for (const val of foo) {
  console.log(`of ${val}`);
}
for (const key of foo) {
  console.log(`in ${key}`);
}
console.log(`entries ${foo.entries().next().value}`);

🙁 Actual behavior

tsc --allowJs input.js --outFile output.js transpiles the code above to this:

var foo = new Set();
foo.add("bar");
console.log(foo.size);
for (var _i = 0, foo_1 = foo; _i < foo_1.length; _i++) {
    var val = foo_1[_i];
    console.log("of " + val);
}
for (var _a = 0, foo_2 = foo; _a < foo_2.length; _a++) {
    var key = foo_2[_a];
    console.log("in " + key);
}
console.log("entries " + foo.entries().next().value);

Set doesn't have a length property, nor can it be indexed by integers. This code runs, logs the size as 1, skips both loops, then logs "entries bar, bar".

🙂 Expected behavior

No change to this code during transpilation.

Additional information about the issue

Even when setting the playground to enter Javascript code, it seemed to still compile it as Typescript code rather than using the Javascript transpiler. I was not able to reproduce this in the playground.

I am using tsc as a transpiler to convert output from Bun (which only targets recent ES standards) to run in older Node.js environments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions