【发布时间】:2018-05-25 12:50:07
【问题描述】:
对不起,这个奇怪的标题,我不太知道如何用一句话来描述我想做的事情。
我必须定义一堆类,它们都将从这个类扩展并实现另一个类。
class SoulCoughing extends Super implements BonBon { /.../ }
class MoveAside extends Super implements BonBon { /.../ }
class LetTheManGoThru extends Super implements BonBon { /.../ }
我编写了一种包装函数,用作这些类的装饰器。
const Eminem = function(klass: Constructable<????>) {
const instance = new klass();
// Do stuff
}
Constructable 是我正在使用的一个小接口,否则 TypeScript 会抛出关于没有构造函数的错误。
interface Constructable<T> {
new(): T;
}
现在这是我的问题,我不知道在我的包装函数中为参数klass 分配什么类型?我试过这样做:
... function(klass: Contrusctable<Super & BonBon>)
还有这个:
... function(klass: Contrusctable<Super | BonBon>)
我也尝试像这样修改我的可构造界面:
interface Constructable<T, U> {
new(): T & U;
}
... function(klass: Contrusctable<Super, BonBon>)
但我不断收到Argument of type 'typeof SoulCoughing' is not assignable to parameter of type 'Constructable<everythingIveTriedSoFar>' 错误。
所以我的问题是,我应该对参数klass 使用什么类型定义?我知道我可以只使用any,但我真的很想确保所传递的类扩展了Super 并实现了BonBon。
【问题讨论】:
标签: class typescript extends implements