Skip to content

Compilation of function indirection leads to runtime error #10267

@westy92

Description

@westy92

TypeScript Version: 2.1.0-dev.20160810

Code
Tried with --target es5 and --target es6

'use strict';

class O {
  constructor(private n: string) { }
  public getA = () => this.n;
  public getZ() { return this.n; }
}

let o = new O('5');

let getB = () => '5';
let getC = () => o.getZ();
let getD = function() { return o.getZ() };

console.log(o.getA());
console.log(getB());
console.log(getC());
console.log(getD());
console.log(o.getZ());
console.log();

function indirect(f: () => string) {
  return f();
}

console.log(indirect(o.getA));
console.log(indirect(getB));
console.log(indirect(getC));
console.log(indirect(getD));
console.log(indirect(o.getZ.bind(o)));
console.log(indirect(o.getZ));

Expected behavior:
A warning or error on transpilation because this will be undefined.
OR
A warning or error on transpilation because we're passing a class instance method as a function parameter.
Actual behavior:
Transpiles and then on run:

5
5
5
5
5

5
5
5
5
5
/home/seth/Desktop/test.js:8
    O.prototype.getZ = function () { return this.n; };
                                                ^

TypeError: Cannot read property 'n' of undefined
    at O.getZ (/home/seth/Desktop/test.js:8:49)
    at indirect (/home/seth/Desktop/test.js:21:12)
    at Object.<anonymous> (/home/seth/Desktop/test.js:28:13)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:148:18)
    at node.js:405:3

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions