打字稿覆盖Mixin中的构造函数参数答案

您所在的位置:网站首页 vue使用mixin 打字稿覆盖Mixin中的构造函数参数答案

打字稿覆盖Mixin中的构造函数参数答案

#打字稿覆盖Mixin中的构造函数参数答案| 来源: 网络整理| 查看: 265

在我当前的打字稿项目中,我正在尝试创建一个 mixin,以便我可以创建多个从不同基类继承的子类。

这一切都很好,但我似乎无法弄清楚如何告诉打字稿新的派生类具有与基类不同的参数。这是一个示例,说明了我在这里尝试做的事情

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' // Argument of type '{ bar: string; qux: string; }' is not assignable to parameter of type 'ConstructorFoo'. // Object literal may only specify known properties, and 'qux' does not exist in type 'ConstructorFoo'. })

但我收到以下错误,因为我不知道如何指定 class Baz 具有不同的参数类型:

Argument of type '{ bar: string; qux: string; }' is not assignable to parameter of type 'ConstructorFoo'. Object literal may only specify known properties, and 'qux' does not exist in type 'ConstructorFoo'.

感谢您的帮助,这里是a playground link detailing exactly what I want to do



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3