Mike Graham
Mike Graham

Reputation: 1044

TypeScript definition for factory function

I'm seeking guidance on creating a class declaration in a .d.ts file.

The class has a method that accepts a typeof T and returns an instance of T.

Upvotes: 3

Views: 2023

Answers (1)

basarat
basarat

Reputation: 275799

You need something that is creatable and then its smooth sailing:

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

Upvotes: 10

Related Questions