In my code, I have a class named Entity as shown below:
class Entity {
constructor(readonly someValue: string) {}
someFunction() {}
}
Now, I am trying to create a class that will handle these entities and be able to create instances of them. In order to allow for the possibility of deriving these entities in the future, I want to make the entity class a generic class argument like this:
class Manager1<T extends Entity> {
create() {
const instance = new T("theValue"); // ERROR: 'T' only refers to a type, but is being used as a value here.
}
}
This results in the mentioned error. I also attempted another approach (which does not work either):
class Manager2<T extends new (someValue: string) => Entity> {
create() {
const instance = new T("theValue"); // ERROR: 'T' only refers to a type, but is being used as a value here.
}
}
Is there a way to instantiate an Entity object inside the class, using only the TYPE as a generic argument (perhaps by enforcing that the constructor of that type must match the specified signature)?