Closed
Description
π Search Terms
ES5, noCheck, loop, closures, transpilation, transpileModule
π Version & Regression Information
This broke in either #57934 or #58364
β― Playground Link
π» Code
// @noCheck: true
// @target: es5
// @showEmit
const fns = [];
for (const value of [1, 2, 3]) {
fns.push(() => ({ value }));
}
const result = fns.map(fn => fn());
console.log(result)
π Actual behavior
The following incorrect code is produced:
"use strict";
var fns = [];
for (var _i = 0, _a = [1, 2, 3]; _i < _a.length; _i++) {
var value = _a[_i];
fns.push(function () { return ({ value: value }); });
}
var result = fns.map(function (fn) { return fn(); });
console.log(result);
Which evaluates to this incorrect value: [{ value: 3 }, { value: 3 }, { value: 3 }]
π Expected behavior
The following should be produced:
"use strict";
var fns = [];
var _loop_1 = function (value) {
fns.push(function () { return ({ value: value }); });
};
for (var _i = 0, _a = [1, 2, 3]; _i < _a.length; _i++) {
var value = _a[_i];
_loop_1(value);
}
var result = fns.map(function (fn) { return fn(); });
console.log(result);
Which would evaluate to: [{ value: 1 }, { value: 2 }, { value: 3 }]
Additional information about the issue
transpileModule(ts, { compilerOptions: { target: ScriptTarget.ES5 } })
started producing incorrect code in TypeScript 5.5, since I believe noCheck: true
is the default there.