Skip to content

conversion from loop body to function munges keyword object properties #13586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sciolizer opened this issue Jan 20, 2017 · 0 comments · Fixed by #13600
Closed

conversion from loop body to function munges keyword object properties #13586

sciolizer opened this issue Jan 20, 2017 · 0 comments · Fixed by #13600
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue High Priority

Comments

@sciolizer
Copy link

TypeScript Version: 2.1.5

Code

type MyType = {
    arguments: Array<string>
}

function myFunction(myType: MyType) {
    for (let i = 0; i < 10; i++) {
        console.log(myType.arguments[i]);

        // create closure so that tsc will turn loop body into function
        const x = 5;
        [1, 2, 3].forEach(function(j) {
            console.log(x);
        })
    }
}

export { myFunction };

Expected behavior:

myType.arguments[i] is left unmodified or translated to something equivalent

Actual behavior:

myType.arguments[i] becomes myType.arguments_1[i].

"use strict";
function myFunction(myType) {
    var _loop_1 = function (i) {
        console.log(myType.arguments_1[i]);
        // create closure so that tsc will turn loop body into function
        var x = 5;
        [1, 2, 3].forEach(function (j) {
            console.log(x);
        });
    };
    var arguments_1 = arguments;
    for (var i = 0; i < 10; i++) {
        _loop_1(i);
    }
}
exports.myFunction = myFunction;

ES5 permits keywords to be used as object properties.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Reserved_word_usage

If in aspiring to be a superset of javascript, typescript also wants to allow keywords to be used as object properties, then myType.arguments[i] should stay unchanged or become something equivalent. If not, then typescript should raise an error. Currently no error is raised, and incorrect javascript is generated.

@mhegazy mhegazy added the Bug A bug in TypeScript label Jan 20, 2017
@mhegazy mhegazy added this to the TypeScript 2.2 milestone Jan 20, 2017
@vladima vladima added the Fixed A PR has been merged for this issue label Jan 21, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue High Priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants