Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. What I need is a way to hide some properties from the BehaviorSubject superclass, for example, the next() method.

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?

like image 806
undefined Avatar asked Dec 22 '17 06:12

undefined


2 Answers

No. You can override the method in the subclass to do something else (or nothing) but this is violating the Liskov Substitution principle.

If your Store class is not a BehaviorSubject, and does not act like one, then extending is not correct. A Store should contain a private instance of BehaviorSubject in this case, and if necessary expose some of its methods/properties, by “proxying” them.

like image 141
Thiago Barcala Avatar answered Nov 14 '22 03:11

Thiago Barcala


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). It's good on its own, because it decouples classes and also makes StoreWrapper testable.

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. 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).
like image 41
Igor Soloydenko Avatar answered Nov 14 '22 03:11

Igor Soloydenko