TypeScript for factory function

I am looking for a guide on how to create a class declaration in a .d.ts file.

The class has a method that takes a type T and returns an instance of T.

+3


source to share


1 answer


You need something creative and then it floats smoothly:



interface Creator<T> {
    new (): T;
}
function factory<T>( arg: Creator<T> ): T {
    return new arg();
}


// Usage: 
class Foo {
    something = 123;
}
var foo = factory( Foo ); // foo:Foo

      

+10


source







All Articles