Reputation:
I set out to write a generic TypeScript function that takes a class constructor as an argument, as well as any arguments required by the constructor, and returns a new instance of the class. One of my requirements is that the return type and constructor argument types must be inferred by the compiler.
After some trial and error I came up with a working version that seems to do what I need (see below). But, is there a better way to achieve this?
function instantiate<Params extends any[], Instance>(
ctor: new (...args: Params) => Instance, ...args: Params
) {
return new ctor(...args);
}
Example usage:
class Circle {
constructor(public radius: number) {}
}
class Rectangle {
constructor(public width: number, public height: number) {}
}
const circle = instantiate(Circle, 5);
const rectangle = instantiate(Rectangle, 3, 7);
Upvotes: 5
Views: 444
Reputation: 43098
You can make it simpler in terms of your use of generic types by making use of the ConstructorParameters
and InstanceType
utility types:
function instantiate<T extends new (...args: any[]) => any>(
ctor: T, ...args: ConstructorParameters<T>
): InstanceType<T> {
return new ctor(...args);
}
Upvotes: 4