microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.29k stars 12.53k forks source link

Down-level destructuring in `for..in` statement #3715

Open rbuckton opened 9 years ago

rbuckton commented 9 years ago

We report an error for the following code, :

for (var {toString} in { a: 1 }) { // error TS2491: The left-hand side of a 'for...in' statement cannot be a destructuring pattern.
   console.log(toString);
}

We also emit the following incorrect javascript:

for (var toString = (void 0).toString in { a: 1 }) {
    console.log(toString);
}

We should instead emit:

for (var _a in { a: 1 }) {
    var toString = _a.toString;
    console.log(toString);
}

While it is unlikely this will be very useful, this is another of the ES6 compatibility tests on the kangax compatibility table that we could consider addressing as it seems a small enough change.

RyanCavanaugh commented 9 months ago

The PR seems fine but maybe for simplicity let's just say it's still an error in scenarios where it would need to be downleveled? That way we're not ballooning the ES5 transform with something that ~no one will ever use.