Can we use reflection in Type Script just like C# to get the list of classes which implement a certain base class?
For example, let say Snake and Horse implement the
One way to do it would be to keep track of all the classes you create in one global array:
var allClasses = []
function register(c) {
allClasses.push(c);
}
register(class Animal {});
register(class Snake extends Animal {});
register(class Horse extends Animal {});
register(class Car {});
and when you need classes that inherit from particular class just filter out the ones you want:
Object.values(allClasses).filter((c) => Animal.isPrototypeOf(c))
This solution is not specific to Typescript, it'll work with pure JavaScript too.