I'm currently working on creating a function that takes a class (Clazz
) as a parameter and returns an instance of the same class, like this:
function createInstance(Clazz) {
...
return new Clazz();
}
Is there a way to automatically determine the type of Clazz
and use it as the return type for this function? I know that I can achieve this with generics like this:
function createInstance<T>(Clazz: new () => T): T {
...
return new Clazz();
}
var instance = createInstance<myClass>(myClass)
However, I want to avoid repeating myClass
as shown in the example above.