3

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.

1
  • Take a look at the WidgetFactory example typescriptlang.org/Handbook#writing-dts-files-examples. If you already have a .ts code for the class with the factory method... then just use the --declaration command line option of the tsc compiler and it will spit out the .d.ts file for you Commented Dec 31, 2014 at 4:40

1 Answer 1

10

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.