Basically, I have the following TypeScript code:
export class ClassA {
method1() {
this.method2(new ClassB().method3);
}
method2(functionReference: () => void) {
functionReference();
}
}
export class ClassB {
method3() {
this.method4();
}
method4() {
console.log("method4 invoked");
}
}
Being more familiar with other OO programming languages (Java, C#, C++), I would expect that running new A().method1();
would eventually cause method4
in ClassB
to be invoked. Instead I run into the problem, that this
is undefined when method3
is eventually run and the execution fails:
TypeError: Cannot read properties of undefined (reading 'method4')
I am aware of all the trickery needed to get this
to work in JavaScript, but I had hoped for the behaviour to be more intuitive in TypeScript. As long as method3
is a non-static method, I would expect to be able to reference this
in the method, no matter from where the method is invoked.
Is there a not too cumbersome pattern available in TypeScript allowing me to use non-static methods as callback functions like I am trying to do here, or is it only possible to use static methods? The latter does not really sound correct, because if it had been so, I would expect the compiler to fail on this.method2(new ClassB().method3)
where I try to pass a non-static method as a function reference.