【发布时间】:2018-05-23 17:48:04
【问题描述】:
我无法弄清楚如何在不求助于一些 hack 的情况下获取 typescript mixin 类的类型(如下所示)
type Constructor<T = {}> = new(...args: any[]) => T;
function MyMixin<T extends Constructor>(BaseClass: T) {
return class extends BaseClass {doY() {}}
}
// Option A: Ugly/wasteful
const MixedA = MyMixin(class {doX() {}});
const dummy = new MixedA();
type MixedA = typeof dummy;
class OtherA {
field: MixedA = new MixedA();
a() {this.field.doX(); this.field.doY();}
}
// Option B: Verbose
class Cls {doX() {}}
interface MixinInterface {doY(): void}
const MixedB = MyMixin(Cls);
type MixedB = Cls & MixinInterface;
class OtherB {
field: MixedB = new MixedB();
a() {this.field.doX(); this.field.doY();}
}
typescript 不支持诚实的 mixins/traits 让我感到非常难过,但是有没有其他方法可以声明 field 的类型,而无需使用 typeof 实例或不必在接口中重新声明签名(我试过typeof(new MixedBaseA()),但 typeof 不接受任意表达式)?
【问题讨论】:
-
这与typescriptlang.org/docs/handbook/mixins.html 上的建议不同,例如
class SmartObject implements Disposable, Activatable -
他们在 2.2 [1] 中引入了对 mixin 类的支持。我认为文档不是最新的 (1: typescriptlang.org/docs/handbook/release-notes/…)
-
@sky 然后不要使用注释。你不需要一个。让编译器推断类型
-
@AluanHaddad 在给出的示例中没问题,但是在更复杂的设置中,我需要在构造函数中实例化字段(因为它取决于某些构造函数参数),而不是指定类型将导致 tsc 推断
any而不是 mixin 类类型 -
@sky 说得好。但是,如果构造函数参数也被捕获为字段,您可以在字段初始化程序中引用它
标签: typescript