I have just started exploring the OOP paradigm and I am curious to know if it is possible to have conditional inheritance in TypeScript. This would help avoid repeating code. Here is what I have in mind. Any suggestions or recommendations are greatly appreciated.
interface Person {
name: string;
age: number;
}
interface Animal {
genre: string;
age: number;
}
abstract class Base {
private velocity: number = 1;
run() {
// if Person extends base return velocity * 0.2; ??
return this.velocity * 0.4;
}
}
class Human extends Base implements Person {
...
}
class Dog extends Base implements Animal{
...
}
let marie = new Human('Marie', 22);
marie.run() //should be 0.2
let bennie = new Dog(...);
bennie.run() // should be 0.4
Is there a way to make this work, or is the only option to declare the method as abstract in the base class and then implement it separately for each case?