Automatically infer type of constructor

  Kiến thức lập trình

Is there any way to get constructor type from A class in this setup without creating a separate constructor type?

class A{
  constructor(a1: string, a2: number){

  }
};
class B extends A{};

async function factory<T extends A>(classFn: { new (...args: any[]): T }){ // <--- here i'd like to have the proper signature inferred
  return new classFn;
}

async () => {
  const b = await factory(B);
}

Playground

LEAVE A COMMENT