标题可能具有误导性,但我真的不知道如何表达我的问题。
我想提供一个泛型类(或只是类体)作为函数参数。然后提供的类应该推断出它的泛型。
class Builder<EXT, INT = EXT> {
// some builder stuff, which changes the generic T
withBar(): Builder<EXT, INT & { bar: string }> {
return this as any;
}
// here's the problem:
build(clazz: MyClass<INT>): MyClass<EXT> {
return wrap(clazz); // this method already exists and works
}
}
用法:
const builder = new Builder<{ foo: number }>();
// EXT = { foo: number }, INT = { foo: number }
builder = builder.withBar();
// EXT = { foo: number }, INT = { foo: number, bar: string }
builder.build(class { /* here I should be able to access this.foo and this.bar */ });
// Here I just want to provide the class body,
// because I don't want to type all the generics again.
// I don't want to specify `MyClass<?>`,
// because the correct type is already specified within the Builder
作为一个(丑陋的)“解决方法”,我找到了一种方法来分别提供所有类方法,然后从中构建一个类。像这样的东西:
class Builder<EXT, INT = EXT> {
build(args: {method1?: any, method2?: any}): MyClass<EXT> {
class Tmp {
method1() {
return args.method1 && args.method1(this);
}
method2(i: number) {
return args.method2 && args.method2(this);
}
}
return wrap(Tmp);
}
}
但这真的很丑。
基本上我真的只想为build
方法提供类体。然后这个方法将从它创建一个类,调用wrap
并返回。
有没有办法做到这一点?
编辑:另一个尝试解释我的问题:
目前我必须使用这样的代码:
builder.build(class extends AbstractClass<{ foo: number, bar: string }> {
private prop: string;
init() {
this.prop = this.data.foo // provided by AbstractClass
? 'foo'
: 'bar'
}
getResult() {
return {
foo: this.prop,
bar: this.data.bar // provided by AbstractClass
}
}
})
如您所见,我必须指定AbstractClass
. 我不想指定类型,因为builder
已经知道类型。
我只想提供类的主体而不再次指定泛型类型。像这样的东西:
builder.build(class extends AbstractClass<infer the type with magic!> {
...
getResult() {
return { this.data.foo }
}
})
或这个:
builder.build(class {
...
getResult() {
return { this.data.foo }
}
})