Refer to a class definition instead of one of its instances in TypeScript
In TypeScript, when using a class as a type, it types as an instance of this class.
ts
classAnimal {}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.
ts
classAnimal {}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.