R3Tech R3Tech - 1 month ago 7
TypeScript Question

Inheritance static member seperate for each class

The class A and B are holding the same adress reference to the config member. How can I seperate them?

class ConfigModel {

public static config = []

public foo() {
//Code
}

}

class A extends ConfigModel {

}

class B extends ConfigModel {

}

Answer

You may want to try the following:

class ConfigModel {
  static get config () {
    return this._config = this._config || [];
  }
}

class A extends ConfigModel {
}

class B extends ConfigModel {
}

A.config.push(1);
B.config.push(2);

console.log(A.config); // [1]
console.log(B.config); // [2]

All it does it to define the static property on the fly once accessed for the first time. Hope it helps!

Comments