Skip to content
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

Compilation of function indirection leads to runtime error #10267

Closed
westy92 opened this issue Aug 10, 2016 · 1 comment
Closed

Compilation of function indirection leads to runtime error #10267

westy92 opened this issue Aug 10, 2016 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@westy92
Copy link

westy92 commented Aug 10, 2016

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
@gcnew
Copy link
Contributor

gcnew commented Aug 11, 2016

If you add this annotations, it will be an error:

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

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

let o = new O('5');
console.log(indirect(o.getZ)); // Argument of type (this: O) => string is not assignable ...

Sadly automatic inferring of this didn't make it, because of negative performance implications :(. For further information see #6739

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 11, 2016
@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants