Is there a way to reference the current class type in the type signature? This would allow me to implement something like the following:
export class Component{
constructor(config?: { [field in keyof self]: any }) {
Object.assign(this, config)
}
}
The concept is to pass a configuration object that includes keys from the current class.
I could use interfaces for this purpose, but then I would have to repeat the same code in both the interface and the implementing class.
Another approach would be to utilize generics. For example:
export class Component<T>{
init(config?: { [field in keyof T]?: any }) {
Object.assign(this, config)
}
}
class TestComponent extends Component<TestComponent>{
foo: number
}
const component = new TestComponent().init({ foo: 11 })
However, the need to write
class TestComponent extends Component<TestComponent>
prompts me to explore alternative solutions...