简体   繁体   English

Typescript-使用类作为接口,为什么我必须实现私有成员/方法?

[英]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.

相关问题 TypeScript类实现接口中的私有方法 - Private methods in TypeScript class implementing interface 访问使用接口实例化的类的私有成员 - Access private members of a class instantiated using interface 接口继承-从父接口隐藏方法-为什么在实现类中可以同时使用这两种方法? - Interface inheritance - hiding method from parent interface - why can I have both methods in the implementing class? 为什么子类需要实现父接口来隐藏C#中的父类方法之一? - Why do a child class need to implement a parents interface to hide one of that parents methods in C#? 接口和类有什么区别,为什么我可以在类中直接实现方法时使用接口? - What is the difference between an interface and a class, and why I should use an interface when I can implement the methods directly in the class? 我是否具有由我正在实现的接口继承的实现方法和变量? - Do I have implement methods and variables which are inherited by the interface I am implementing? class中的接口方法没有实现吗? - Interface methods in a class that does not implement it? 接口方法的成员具有不同的类型 - Members of interface's methods have different types 在Java中继承抽象类时是否需要再次实现接口的所有方法? - Do i need to implement all the methods of interface again while inheriting abstract class in Java? 为什么我不能拥有受保护的接口成员? - Why can't I have protected interface members?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM