Let's suppose I'm looking to reuse some basic constructor in subclasses but its parameter will depend on the properties of the subclass.
Here is a basic (non-working) example of what I'm trying to achieve.
class Foo {
a: string = 'a'
constructor(params?: { [key in keyof this]: this[key] }) { // this doesn't work as 'this' is not permited static members.
Object.assign(this, params)
}
}
class Bar extends Foo {
b: string = 'b'
// constructor inherited accepting { a: string, b: string }
}
const myFoo = new Foo({ a: 'foo' })
const myBar = new Bar({ a: 'foo', b: 'bar' })
Is there some kind of way to do it in TS ?