Alexey Vagarenko Alexey Vagarenko - 1 month ago 4
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



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