Check out this code snippet:
class Parent {
constructor() {
}
// this method is called from child,
// so 'this' is type of Child and cosmisFunc exists
callChildFunction() {
this.cosmisFunc() // compiler error here: Property 'cosmicFunc' does not exist on type 'Child'.ts(2339)
}
}
class Child extends Parent {
constructor() {
super();
}
someMethod() {
this.callChildFunction()
}
cosmisFunc() {
console.log('oh mama')
}
}
//not meaningfull code... instance construction...
myChildInstance.someMethod()
I need to find a way in typescript to inform the compiler that a method will be called from a child class in a parent class, to avoid the ts(2339) error. TypeScript currently restricts calling callChildFunction
with a different this
, although it's allowed in JavaScript. Perhaps there should be a way in TypeScript to specify that this
will always stay within the inheritance chain (ensuring it references a Parent
or Child
instance).