vendredi 20 mars 2020

Pattern or construct to set default values for multiple classes implementing an interface

I find myself repeating a lot of code, along the lines of:

interface IMyInterface {
  commonA: string;
  commonB: string;
}

class Foo implements IMyInterface {
  commonA: string = "hello";
  commonB: string = "world";
  foo: string = "Foo!";
}

class Bar implements IMyInterface {
  commonA: string = "hello";
  commonB: string = "world";
  bar: string = "Bar!";
}

In my case I have numerous classes which all share commonA and commonB but differ otherwise. Now, I know I can't set default values in the interface, but is there some design pattern or TS construct which will allow me to define the default values in one place?

Aucun commentaire:

Enregistrer un commentaire