Multiple classes that have a different name but extends the same class

I have for example this class:

abstract class MyClass {
  abstract myProp: number;

  constructor() {
    // Some code
  }
}

So I want to create multiple classes that extends this class. But I don’t want to repeat it multiple times as I will have a lot of classes. So the purpose is that each class has a different name and myProp.

For example:

class FirstClass extends MyClass {
  myProp = 1;
  constructor() {
    super();
    // Some code
  }
}

class SecondClass extends MyClass {
  myProp = 2;
  constructor() {
    super();
    // Some code
  }
}

So I want to generate these classes (with for example a function) but the problem is that I will have to use the new keyword.
So the usage for each of these classes should be like this:

const myConst = new FirstClass();
const myConst2 = new SecondClass();

I hope this makes some sense. I just don’t want to repeat every class because it has a different name and myProp.

48 thoughts on “Multiple classes that have a different name but extends the same class”

  1. 599501 938305Im impressed, I must say. Genuinely rarely do you encounter a weblog thats both educative and entertaining, and let me tell you, you might have hit the nail about the head. Your concept is outstanding; ab muscles something that too couple of folks are speaking intelligently about. Im delighted i located this in my hunt for something about it. 170155

    Reply

Leave a Comment