[英]Typescript- Using class as interface, why do I have to implement private members/methods?
I'm using the Mixin pattern as illustrated below. 我正在使用如下所示的Mixin模式。 Why does Typescript require you to provide stand-in properties for private properties of the mixin class in the target class (A)?
为什么Typescript要求您为目标类(A)中mixin类的私有属性提供替代属性? It perfectly makes sense for the public properties, but for private properties it unnecessarily crufts up the target class with details of the internal implementation of the mixin class by requiring them to be stubbed-out in the target class.
对于公共财产来说,这是完全合情合理的,但对于私有财产,它不必要地通过要求将mixin类的内部实现细节塞入目标类中,从而使目标类变得多余。 Seems like the Typescript transpiler should be able to not require this.
好像Typescript编译器应该不需要这个。
class Mixin {
private foo:string;
}
class A implements Mixin {
// stand-in properties, typescript requires even
// private properties to be stubbed-out
foo:string;
}
Private members contribute to the structure of a type in TypeScript, so if you don't implement them you are not compatible with the type. 私有成员有助于TypeScript中类型的结构,因此,如果不实现它们,则与该类型不兼容。 This actually makes it impossible to match a type structurally in TypeScript if it has a private member, because you are either:
实际上,如果拥有私有成员,实际上就不可能在TypeScript中进行结构上的匹配,因为您是:
a. 一种。 Failing to provide the type
无法提供类型
or 要么
b. 湾 Providing a separate implementation of the private member
提供私有成员的单独实现
So you can only extend
a type with a private member, not implement
it. 所以,你只能
extend
了私人成员的类型,而不是implement
它。
With this in mind, you are better off not using private members with mixins. 考虑到这一点,最好不要将私有成员与mixins一起使用。 Provide the ghost-members in the implementation class and keep your fingers crossed that if mixins gain some traction the ghosting will become unnecessary (see TypeScript mixins part one ).
在实现类中提供幽灵成员,让您的手指保持交叉,如果mixins获得一些吸引力,则不需要重影(请参阅TypeScript mixins第一部分 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.