I am seeking a way to access the static members of classes, including parent classes from which an object was created. Currently, I am adding each class to an array in the constructor as a workaround. However, with thousands of classes to define, I am hoping for a more elegant solution or a method to restrict the Type to the master class.
Below is some sample code illustrating my point:
type Class = { new(...args: any[]): any; }
class Animal {
static description = "A natural being that is not a person";
classes: Class[] = [];
constructor() {
this.classes.push(Animal)
}
}
class Mammal extends Animal {
static description = "has live births and milk";
constructor() {
super(); // adds Animal to classes
this.classes.push(Mammal)
}
}
class Dog extends Mammal {
static description = "A man's best friend";
constructor() {
super(); //adds Animal and Mammal to classes
this.classes.push(Dog)
}
}
class Cat extends Mammal {
static description = "A furry purry companion";
constructor() {
super(); //adds Animal and Mammal to classes
this.classes.push(Cat)
}
}
let fido = new Dog()
fido.classes.forEach(function(i) {
console.log(i.description)
}
I would like classes to only accept instances of Animal and its extensions.