Typescript 在混入中重写构造函数参数
Typescript 在混入中重写构造函数参数
在我的当前typescript项目中,我正在尝试创建一个mixin,以便可以创建多个子类,这些子类继承自不同的基类。
一切都运行良好,但我似乎无法弄清楚如何告诉typescript新派生类具有不同的参数而不是基类。这里有一个示例,说明了我在这里尝试做什么。
interface ConstructorFoo { bar: string, } class Foo { public bar: string constructor({ bar }: ConstructorFoo) { this.bar = bar } } interface ConstructorBaz extends ConstructorFoo { qux: string } type FooType = new (...args: any[]) => Foo const quxMixin =(base: T) => { return class Baz extends base { public qux: string constructor (...args: any[]) { super(...args) const { qux } = args[0] as ConstructorBaz this.qux = qux } } } const FooBaz = quxMixin(Foo) const q = new FooBaz({ bar: '1', qux: '2' // 类型“{ bar: string; qux: string; }”的参数不能赋给类型“ConstructorFoo”的参数。 // 对象文字只能指定已知属性,并且“qux”不存在于类型“ConstructorFoo”中。 })
但是我得到了以下错误,因为我不知道如何指定`class Baz`具有不同的参数类型:
`类型“{ bar: string; qux: string; }”的参数不能赋给类型“ConstructorFoo”的参数。
对象文字只能指定已知属性,并且“qux”不存在于类型“ConstructorFoo”中。`
感谢您的帮助,这里是一个详细说明我想要做的一个示例。