How to upcast to limit object properties

时间:2018-03-25 19:14:54

标签: javascript

In JavaScript, how can I upcast from a subclass to superclass to automatically remove object properties that don't exist in superclass?

Example: assume the following 2 classes:

class ClassA {
  constructor(public a: string, public b: string) { }
}

class ClassB extends ClassA {
  constructor(public a: string, public b: string, public c: string) {
    super(a, b);
  }
}

Create an instance of ClassB:

let objB = new ClassB('a', 'b', 'c');

How can I strip fields that don't exist in ClassA from objB without specifying each field to be copied or removed? (ie. using spread or rest operators)

For example, in Java, one would cast to the superclass as below:

objA = (ClassA) objB;

0 个答案:

没有答案