I have a class with some property/methods that return value which was orignially passed via the constructor:
class Base {
constructor(arg) {
this.options = arg.options;
}
}
People suggest this class with generics as the typescript equivalent:
class Base<T extends {options: any}> {
options: T["options"];
constructor(arg: T) {
this.options = arg.options;
}
}
// works fine with instantiation
const inst = new Base({options: {foo: 123}});
inst.options.foo++; // ok
inst.options.bar; // TS Error
Now i have a sub class which calls the super class's constructor and passes these options:
class Sub extends Base { // TS Error
constructor() {
super({options: {foo: 123}});
this.options.foo++; // TS Error
}
}
Typescript compiler isn't happy with this and wants the Base class being extended passing the type. But then i have the information twice, in the super call and the extends notation. What is the correct ts syntax to solve my issue?
(code in typescript playground)