Refer to a class definition instead of one of its instances in TypeScript
Published on
In TypeScript, when using a class as a type, it types as an instance of this class.
tsclassAnimal {}letdog :Animal // `dog` is typed as an instance of the `Animal` class
To be able to type as the class definition itself, and not one of its instances, we need to use the keyof operator.
tsclassAnimal {}letclassDefinition : typeofAnimal
Declaring a class in TypeScript creates two things: a constructor (the class definition, aka. typeof Animal) and an interface for instances of the class (the type Animal).
I found the solution to my problem on 2ality article about classes as values in TypeScript.