You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I noticed TypeScript incorrectly transpiles the following code to ES5/ES3 where a closure is used in a for loop "header" referencing the block-scoped loop variable. This probably doesn't occur in real-world code, however:
I just found something similar in 2.4.2, might be the same issue:
const items = ["a", "b", "c"]
const callbacks = []
const collected: string[] = []
for (const index in items) {
callbacks.push(() => {
// index // uncomment to workaround bug
collected[index] = "present"
})
}
for (const callback of callbacks) callback()
console.log(collected)
Basically this example iterates over the indices of an array, capturing them in callbacks, which are then executed. These then use that index to populate a sparse array.
Without the workaround, this produces ", , present". With the workaround (just evaluating the index outside of an array accessor, not using it for anything) this produces "present, present, present".
TypeScript Version: 2.5.0-dev.20170803
Hi, I noticed TypeScript incorrectly transpiles the following code to ES5/ES3 where a closure is used in a
for
loop "header" referencing the block-scoped loop variable. This probably doesn't occur in real-world code, however:Code
Expected output (when running in Node, Chrome, Firefox or Edge):
Actual output (after transpiling to ES5):
Actual transpiled code:
I'm not sure how the correct emitted code should ideally look like - maybe similar to this attempt (which produces the correct result):
Thanks!
The text was updated successfully, but these errors were encountered: