Type instantiate(Ctor, ...args) so it accepts any class with matching arguments and returns its instance.
instantiate calls new Ctor(...args). Type it so the arguments are checked against that class's constructor, the result is typed as that class's instance, and passing something that isn't constructable is an error.
function instantiate(Ctor: Function, ...args: unknown[]) { return new Ctor(...args);} // class Point { constructor(public x: number, public y: number) {} }// const point = instantiate(Point, 1, 2); // Point// instantiate(Point, "1", 2); // should be a type errorThe answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…