I have a collection of different animal classes, all derived from a common abstract base class. To illustrate:
abstract class Animal {
abstract speak(): string;
}
class Dog extends Animal {
speak(): string {
return "woof... sigh"
}
}
class Cat...
All these implementations are stored in an array:
const all = [Cat, Dog];
I want to specify to TypeScript that each item in this array will result in a class instance of Animal when instantiated.
It's worth mentioning that we are passing around the class reference itself, not the object instance – so directly inheriting from the abstract class won't work here.
const all : Array<Animal> = [Cat, Dog]
Strangely enough, I haven't been able to locate any information in the documentation regarding type hinting with the 'new' keyword.