简体   繁体   English

打字稿隐藏了超级类的道具

[英]Typescript hide props from super class

I have a class that extends another class. 我有一个扩展另一个类的类。 For example: 例如:

class Store extends BehaviorSubject {
  constructor(obj) {
     super(obj)
  }
}

I also have more classes the extends Store . 我还有更多类扩展Store What I need is a way to hide some properties from the BehaviorSubject superclass, for example, the next() method. 我需要的是一种从BehaviorSubject超类中隐藏一些属性的方法,例如next()方法。

class SomeStore extends Store {

}

// I want to hide from this class some methods, properties
// That exists on the BehaviorSubject class. 
const s = new SomeStore();

There is a way to do this? 有办法做到这一点?

No. You can override the method in the subclass to do something else (or nothing) but this is violating the Liskov Substitution principle. 不可以。您可以覆盖子类中的方法来执行其他操作(或者不执行任何操作),但这违反了Liskov Substitution原则。

If your Store class is not a BehaviorSubject, and does not act like one, then extending is not correct. 如果您的Store类不是BehaviorSubject,并且不像一个,则扩展不正确。 A Store should contain a private instance of BehaviorSubject in this case, and if necessary expose some of its methods/properties, by “proxying” them. 在这种情况下,Store应该包含BehaviorSubject的私有实例,并且如果需要,通过“代理”它们来暴露它的一些方法/属性。

You can write something like the code below. 您可以编写类似下面的代码。 There's also no inheritance, which is good. 也没有继承,这很好。 Passing the Store object via constructor is a way to implement DIP (dependency inversion principle) . 通过构造函数传递Store对象是实现DIP(依赖性反转原则)的一种方法。 It's good on its own, because it decouples classes and also makes StoreWrapper testable. 它本身很好,因为它解耦了类,也使StoreWrapper可测试。

Code

export class StoreWrapper {

  constructor(private _store: Store) { }

  // Expose things that you think are okay to expose...
  getValue = this._store.getValue;
  onCompleted = this._store.onCompleted;
  onError = this._store.onError;
  onNext = this._store.onNext;

  // Anything that is not exposed similar to how it is done above, will be hidden
  // next = this._store.next;

}

Usage 用法

const originalStore = ...; // <--- this is your original `Store` object.

const wrappedStore = new StoreWrapper(originalStore);
const value = wrappedStore.getValue();
// ... and so on

Important Notes : 重要说明

  • While this._store.next() is still possible to invoke from within the StoreWrapper , this code is not violating the LSP . 虽然仍然可以从StoreWrapper调用this._store.next() ,但此代码不违反LSP In other words, it does not break the way inheritance is supposed to be used. 换句话说,它不会破坏应该使用继承的方式。
  • You can now test this StoreWrapper class very easily, in case it grows and gets some "meat" (logic). 您现在可以非常轻松地测试这个StoreWrapper类,以防它增长并获得一些“肉”(逻辑)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿:超类构造函数的成员变量 - typescript: member variables from super class's constructor 从 App class 传递道具到 const function 反应 Typescript - Pass props from App class to const function React Typescript 打字稿:从扩展类调用超级方法会产生类型错误 - (中间值)不是函数 - Typescript: calling super method from extended class gives type error - (intermediate value) is not a function 超类的Javascript方法 - Javascript methods from super class Typescript 错误和扩展运算符由……道具引起? - Typescript Error and the spread operator arising from …props? 使用公共道具的类继承 - 使用 TypeScript 的通用类注释 - Class inheritance with common props - generic class annotations with TypeScript TypeScript / JavaScript:如何扫描所有全局类(并按超类过滤)? - TypeScript / JavaScript: How to scan for all global classes (and filter by super class)? 你能使用超级 class 方法作为装饰器方法吗 - Typescript - Can you use super class methods as decorator methods - Typescript 从班级内部调用道具 - Calling the props from within the class 如何指定使用Typescript道具接口的类函数 - how do you specify a class function to use typescript props interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM