I know this is an old question, but I needed something similar today and came across a message. After some trial and error, I came up with the following solution:
interface Bar { sayHello(name: string); } class Foo implements Bar { sayHello(name: string) { window.alert("Hello " + name); } } function Baz(c: new() => Bar) { return new C(); } var o = Baz(Foo); o.sayHello("Bob");
Basically, interfaces can define a contract only for an instance of an object, so the requirement for the existence of a particular constructor must be satisfied in the function that the constructor will call. This method goes well with generics:
function Baz<T extends Bar>(c: new() => T) { return new c(); } var o = Baz(Foo);
In the above example, the variable "o" will be output as type Foo.
Jonathan Marston Aug 14 '14 at 4:26 2014-08-14 04:26
source share