Alexey Vagarenko Alexey Vagarenko - 1 year ago 73
TypeScript Question

Is there a way to require function parameter to be a constructor of type parameter?

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


Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download