Skip to content

'this' not appropriately captured when downleveling loop with block-scoped variable #5746

Description

@DanielRosenwasser

From #5208 (comment) by @Nemikolh

With version typescript@1.8.0-dev.20151121 this example:

class C {
  constructor(private N: number) {}
  foo() {
    for (let i of [0]) {
      let f = () => i;
      this.N = f();
    }
  }
}

is not correctly transformed. (The _this binding is not created)

Specifically the output is:

var C = (function () {                                    
    function C(N) {                                       
        this.N = N;                                       
    }                                                     
    C.prototype.foo = function () {                       
        var _loop_1 = function(i) {                       
            var f = function () { return i; };            
            this.N = f();                                 
        };                                                
        for (var _i = 0, _a = [0]; _i < _a.length; _i++) {
            var i = _a[_i];                               
            _loop_1(i);                                   
        }                                                 
    };                                                    
    return C;                                             
})();                                                     

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions