Reputation: 1196
Is there a way to write this:
foo<T>(bar: ConstructorOf<T>): T
so this code would be legal:
class Baz {}
foo(Baz); // type param T infered to be Baz
?
Upvotes: 0
Views: 90
Reputation: 51629
Is this what you are looking for?
class Baz {
isBaz = true;
}
function foo<T>(c : {new(): T;}): T {
return new c;
}
console.log(foo(Baz).isBaz); // ok
console.log(foo(String).length); // ok
// console.log(foo(String).isBaz); // error: property isBaz does not exist in type String
This code is a simplified example from the last topic in https://www.typescriptlang.org/docs/handbook/generics.html.
Upvotes: 2