To call an overridden method in base class constructor in TypeScript, we can call the method from the base class’ instance.
For instance, we write
class A {
protected doStuff() {
console.log("Called from A");
}
public callDoStuff() {
this.doStuff();
}
}
class B extends A {
protected doStuff() {
console.log.doStuff();
alert("Called from B");
}
}
const a = new A();
a.callDoStuff();
const b = new B();
b.callDoStuff();
to call callDoStuff
from an A
instance a
, which will log 'Called from A'
.
If we call callDoStuff
from a the B
instance b
, then we see 'Called from B'
logged.
Conclusion
To call an overridden method in base class constructor in TypeScript, we can call the method from the base class’ instance.