问题基本上是如何确保使用高阶元件以典型的JavaScript方式实现类型检查。
Hoc1 = (superclass) => class extends superclass { ... }
class A { ... }
class B extends Hoc1(A) { ... }通过类型检查,我的意思是使用两个最突出的实用程序:TypeScript或流。
到目前为止,我已经在TypeScript中找到了以下代码片段,
interface IAMixin {
aMixedMethod(): void
}
interface IAMixinConstructor {
new(): IAMixin
}
const AHoc: <T>(superclass: T) => T & IAMixinConstructor = (superclass) =>
class extends superclass implements IAMixin {
aMixedMethod() {}
}
class A {
aMethod() {}
}
class B extends AHoc(A) {
bMethod() {}
}
const b = new B();
b.aMixedMethod(); // false-positive: incrorrectly reports as missing method
b.aMethod();
b.bMethod();
b.cMethod(); // this is correctly caught, though如果我用这种方式写混音
const AMixin: (superclass) => typeof superclass & IAMixinConstructor =
(superclass) => class extends superclass implements IAMixin {
aMixedMethod() {}
}然后,它认为superclass是any,而false-负面地忽略了cMethod调用的错误。
这似乎至少在TypeScript中是可能的,因为他们有正确的Object.assign为实例工作。但我需要的是同类型的建筑,除了上课。
还是需要像Ruby类这样的类类型?
发布于 2017-05-13 12:16:40
所缺少的是将AHoc参数定义为类的构造函数类型,而不是实际实例,对于返回的值也是如此:
interface IAMixin {
aMixedMethod(): void
}
const AHoc: <T>(superclass: new () => T) => new () => (T & IAMixin) =
(superclass) => class extends superclass implements IAMixin {
aMixedMethod() { }
}
class A {
aMethod() {}
}
class B extends AHoc(A) {
bMethod() {}
}
const b = new B();
b.aMixedMethod(); // now good
b.aMethod();
b.bMethod();
b.cMethod(); // this is correctly caughthttps://stackoverflow.com/questions/43952253
复制相似问题