如何声明 class
类型,以确保对象是一般类的构造函数?
In the following example, I want to know which type should I give to AnimalClass
so that it could either be Penguin
or Lion
:
class Animal {
constructor() {
console.log("Animal");
}
}
class Penguin extends Animal {
constructor() {
super();
console.log("Penguin");
}
}
class Lion extends Animal {
constructor() {
super();
console.log("Lion");
}
}
class Zoo {
AnimalClass: class // AnimalClass could be 'Lion' or 'Penguin'
constructor(AnimalClass: class) {
this.AnimalClass = AnimalClass
let Hector = new AnimalClass();
}
}
当然,class
类型不起作用,而且无论如何它都太一般了。