class C {
constructor(public f: { (): string }) {
}
}
class B extends C {
constructor() {
super(() => { return this.g(); })
}
private g() {
return "hello";
}
}
alert(new B().f());
Expected result: "Hello" is alerted.
Actual result: calling f() fails with referenceerror.
The problem is that arrow functions are bound by the use of _this. Unfortunately, when there is a super call with no other this references, Typescript skips emitting the _this variable. _this doesn't exist so when the function is called, it fails.
If somehow this is the intended behaviour, the compiler should error on this references in super calls.
Typescript version: 2.1.4
Playground: https://www.typescriptlang.org/play/index.html#src=class%20C%20%7B%0D%0A%09constructor(public%20f%3A%20%7B%20()%3A%20string%20%7D)%20%7B%0D%0A%0D%0A%09%7D%0D%0A%0D%0A%09%0D%0A%7D%0D%0A%0D%0Aclass%20B%20extends%20C%20%7B%0D%0A%09constructor()%20%7B%0D%0A%09%09super(()%20%3D%3E%20%7B%20return%20this.g()%3B%20%7D)%0D%0A%09%7D%0D%0A%0D%0A%09private%20g()%20%7B%0D%0A%09%09return%20%22hello%22%3B%0D%0A%09%7D%0D%0A%7D%0D%0A%0D%0Aalert(new%20B().f())%3B
Source:
Expected result: "Hello" is alerted.
Actual result: calling f() fails with referenceerror.
The problem is that arrow functions are bound by the use of _this. Unfortunately, when there is a super call with no other this references, Typescript skips emitting the _this variable. _this doesn't exist so when the function is called, it fails.
If somehow this is the intended behaviour, the compiler should error on this references in super calls.