让我们假设我希望在子类中重用一些基本的构造函数,但它的参数将取决于子类的属性。
这里有一个基本的(非工作)例子,我试图实现。
class Foo {
a: string = 'a'
constructor(params?: { [key in keyof this]: this[key] }) { // this doesn't work as 'this' is not permited static members.
Object.assign(this, params)
}
}
class Bar extends Foo {
b: string = 'b'
// constructor inherited accepting { a: string, b: string }
}
const myFoo = new Foo({ a: 'foo' })
const myBar = new Bar({ a: 'foo', b: 'bar' })
字符串
在TS中有什么方法可以做到这一点吗?
操场
2条答案
按热度按时间acruukt91#
对于
Foo
,您可以使用params?: Foo
,然后new Foo({a: "ayy"})
将工作,但Bar
将期望Foo
,而不是Bar
。不幸的是,我认为除了在
Bar
中提供一个构造函数之外,没有什么可以改变的。当然,这个构造函数不需要做太多的事情,只要super(params)
就可以了。字符串
Playground链接
如果
params?: typeof this
(或它的其他衍生物)可以工作(并处理子类之间的含义变化),那就太好了,但目前情况并非如此。xmd2e60i2#
也许不是你想要的,但是你可以使用泛型。当定义一个新的子类时,子类类型将作为泛型传递给基类。
字符串
TypeScriptPlayground
有关
NonFunctionProperties
的详细信息,请参阅此