Is it possible to reference current class type in type signature? so that I can do something like this:
export class Component{
constructor(config?: { [field in keyof self]: any }) {
Object.assign(this, config)
}
}
the idea is to pass a configuration object that would consist of current class keys.
I could go with interfaces but then I need to type same portion of code twise (in interface and in implementing class)
Another way would be to use generics. Something like this:
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 })
But having code like class TestComponent extends Component<TestComponent> makes me to search for better ways...
thisas a type. github.com/Microsoft/TypeScript/pull/4910thisnot in constructor is completely fine for me. This is event better to make such kind of things somewhere ininitfunction, since constructor runs before default values are set up.