打字稿通用工厂函数,包括创建对象的参数
Typescript generic factory function including parameters for created object
我正在尝试在 Typescript 中创建一个工厂 class。应设置工厂以创建特定的 class 对象,包括在工厂初始化时设置的默认参数。
这是我到目前为止的想法:
type Constructor<T> = new(...args: any) => T;
class Factory<T> {
private params: ConstructorParameters<Constructor<T>>;
private ctor: Constructor<T>;
init<TCtor extends Constructor<T>>(tConstructor: TCtor,
...params: ConstructorParameters<TCtor>)
{
this.ctor = tConstructor;
this.params = params;
}
create(): T {
return new this.ctor(...this.params);
}
}
class ClassWithParams { constructor(a: number) { } }
const factory = new Factory<ClassWithParams>();
//factory.init(ClassWithParams);
// Compilation error - missing param a
factory.init(ClassWithParams, 1);
console.log(factory.create());
// instance of ClassWithParams
以上代码运行良好 (Typescript playground)。但是,它需要在构建后直接调用 init()
工厂。我无法将 init()
代码移动到构造函数中,因为 init()
依赖于通用参数 TCtor
以便正确地对参数列表进行类型检查。
我可以让 TCtor
成为 class 的泛型参数,但这意味着每次泛型引用 Factory 时都需要无缘无故地提供一个额外的类型参数。
有什么想法可以删除 init()
调用吗?
下面的代码有什么问题
type Constructor<T> = new (...args: any[]) => T;
class Factory<T, C extends Constructor<T> = Constructor<T>> {
params: ConstructorParameters<C>;
ctor: Constructor<T>;
constructor(tConstructor: Constructor<T>,
...params: ConstructorParameters<C>) {
this.ctor = tConstructor;
this.params = params;
}
create(): T {
return new (this.ctor)(...this.params)
}
}
class ClassWithParams { constructor(a: number) { this.a = a; } a: number }
const factory1 = new Factory(ClassWithParams);
const factory2 = new Factory(ClassWithParams, '1');
const factory3 = new Factory(ClassWithParams, 0);
const value = new Factory(ClassWithParams, 0).create().a;
如此看来playground很方便也很正确。
我正在尝试在 Typescript 中创建一个工厂 class。应设置工厂以创建特定的 class 对象,包括在工厂初始化时设置的默认参数。
这是我到目前为止的想法:
type Constructor<T> = new(...args: any) => T;
class Factory<T> {
private params: ConstructorParameters<Constructor<T>>;
private ctor: Constructor<T>;
init<TCtor extends Constructor<T>>(tConstructor: TCtor,
...params: ConstructorParameters<TCtor>)
{
this.ctor = tConstructor;
this.params = params;
}
create(): T {
return new this.ctor(...this.params);
}
}
class ClassWithParams { constructor(a: number) { } }
const factory = new Factory<ClassWithParams>();
//factory.init(ClassWithParams);
// Compilation error - missing param a
factory.init(ClassWithParams, 1);
console.log(factory.create());
// instance of ClassWithParams
以上代码运行良好 (Typescript playground)。但是,它需要在构建后直接调用 init()
工厂。我无法将 init()
代码移动到构造函数中,因为 init()
依赖于通用参数 TCtor
以便正确地对参数列表进行类型检查。
我可以让 TCtor
成为 class 的泛型参数,但这意味着每次泛型引用 Factory 时都需要无缘无故地提供一个额外的类型参数。
有什么想法可以删除 init()
调用吗?
下面的代码有什么问题
type Constructor<T> = new (...args: any[]) => T;
class Factory<T, C extends Constructor<T> = Constructor<T>> {
params: ConstructorParameters<C>;
ctor: Constructor<T>;
constructor(tConstructor: Constructor<T>,
...params: ConstructorParameters<C>) {
this.ctor = tConstructor;
this.params = params;
}
create(): T {
return new (this.ctor)(...this.params)
}
}
class ClassWithParams { constructor(a: number) { this.a = a; } a: number }
const factory1 = new Factory(ClassWithParams);
const factory2 = new Factory(ClassWithParams, '1');
const factory3 = new Factory(ClassWithParams, 0);
const value = new Factory(ClassWithParams, 0).create().a;
如此看来playground很方便也很正确。