I am exploring the idea of creating a specific class type for classes that possess certain properties. For example:
class Cat {
name = 'cat';
}
class Dog {
name = 'dog';
}
type Animal = ???;
function foo(AnimalClass: Animal) {
console.log((new AnimalClass()).name);
}
I am looking to have doSomething
be able to accept any class that includes a string property named name
. The approach I have taken so far involves:
class _Animal {
name: string;
}
type Animal = typeof _Animal;
I am wondering if there is a way to achieve this without having to define a new JavaScript class and simply focus on specifying the type.