Skip to content

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

Closed
@sciolizer

Description

@sciolizer

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issueHigh Priority

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions