That's a nice well-written article with good points.
Software Dev Team Lead
Here's a Software Dev Team Lead's way of doing this:
No need to even determine the type. Instead the type implements the correct method for itself & there is never a need to even know the base type.
Strategy pattern. 😎🤓
Try The Code
Try the code out and see it run at: https://stackblitz.com/edit/typescript-xibe8j?file=index.ts
interface Animal{
Speak();
}
class Cat implements Animal{
Speak(){
alert("meow");
}
}
class Dog implements Animal{
Speak(){
alert("woof");
}
}
class Lion implements Animal{
Speak(){
alert("roar!");
}
}
let a : Animal = new Dog();
a.Speak(); // woof
let allAnimals = [];
allAnimals.push(new Dog());
allAnimals.push(new Lion());
allAnimals.push(new Cat());
allAnimals.push(new Lion());
allAnimals.forEach( a => a.Speak());
9