Suppose I have these classes
abstract class Animal {
public static getSound(): string {
throw new Error("Abstract animal has no sound");
}
}
class Dog extends Animal {
public static getSound(): string {
return "Woof";
}
}
class Cat extends Animal {
public static getSound(): string {
return "Meow";
}
}
// Other animal classes...
And I want to write a function that takes a generic subclass of Animal, i.e. not instance of subclass, as a parameter and calls the corresponding static method getSound. This is what I've tried so far.
interface ClassType<T = any> {
new (...args: any[]): T;
}
const getAnimalSound = <T extends Animal>(type: () => ClassType<T>) => {
return type().getSound(); // Property 'getSound' does not exist on type 'ClassType '.
};
console.log(() => Dog); // should print "Woof"
console.log(() => Cat); // should print "Meow"
But I'm getting the compile error Property 'getSound' does not exist on type 'ClassType'.
The meaning of the error is obvious, I didn't set up the types correctly, . How should I go about doing this, without redesigning the classes?
Thanks all.