I'm facing an issue with my base (generic) classes where the properties are initialized in the constructor. Whenever I try to extend these classes to create more specific ones, I find myself repeating the same parameters from the base class's constructor just to pass them to super. It feels ineffiecient to constantly duplicate these properties every time I extend the class. Is there a better way to handle this? Maybe some approach to automatically inherit the parent class's constructor parameters?
For instance, take a look at this code snippet - notice how in Programmer
's constructor, I have to redundantly mention name
, age
, and height
again only to pass them to super
:
class Person {
name: string;
age: number;
height: number;
constructor(name: string, age: number, height: number) {
this.name = name;
this.age = age;
this.height = height;
}
}
class Programmer extends Person {
languages: string[];
constructor(name: string, age: number, height: number, languages: string[]) {
super(name, age, height);
this.languages = languages;
}
}