将混合输入 class 约束到接口时出错
Error when constraining a mixin input class to an interface
我正在尝试制作一个 mixin 来为某些 classes 提供接口约束,以便我知道输入 class 具有某些属性。
这是我目前拥有的。
name: string;
description: () => string;
}
function mixin<TBase extends Interface>(base: new(...args: any[]) => TBase) {
return class Mixin extends base {
get summary() {
return `${this.name} ${this.description()}`;
}
}
}
我得到的错误是在 class 函数内部的 Mixin,错误是
'Mixin' is assignable to the constraint of type 'TBase', but 'TBase' could be instantiated with a different subtype of constraint 'Interface'.ts(2415)
如何解决此错误?
你可以试试
interface Interface{
name: string;
description: () => string;
}
type GConstructor<T extends Interface> = new (...args: any[]) => T;
function mixin<TBase extends GConstructor<Interface>>(base: TBase) {
return class Mixin extends base {
get summary() {
return `${this.name} ${this.description()}`;
}
}
}
我正在尝试制作一个 mixin 来为某些 classes 提供接口约束,以便我知道输入 class 具有某些属性。
这是我目前拥有的。
name: string;
description: () => string;
}
function mixin<TBase extends Interface>(base: new(...args: any[]) => TBase) {
return class Mixin extends base {
get summary() {
return `${this.name} ${this.description()}`;
}
}
}
我得到的错误是在 class 函数内部的 Mixin,错误是
'Mixin' is assignable to the constraint of type 'TBase', but 'TBase' could be instantiated with a different subtype of constraint 'Interface'.ts(2415)
如何解决此错误?
你可以试试
interface Interface{
name: string;
description: () => string;
}
type GConstructor<T extends Interface> = new (...args: any[]) => T;
function mixin<TBase extends GConstructor<Interface>>(base: TBase) {
return class Mixin extends base {
get summary() {
return `${this.name} ${this.description()}`;
}
}
}