I have a lot of existing classes that require refactoring to utilize an async constructor.
Here's an example:
class ClassA {
constructor(a: number, b: string, c: string) {
//...
}
//...
}
I've included an async create method:
class ClassA {
// added async
static async create(...args: ConstructorParameters<typeof ClassA>){
await loadSameResources()
return new this(...args)
}
constructor(a: number, b: string, c: string) {
//...
}
}
It functions properly, users simply need to replace new ClassA(...)
with (await ClassA.create(...))
, which is straightforward and minimizes necessary changes.
My issue lies in how to avoid specifying 'ClassA' within the create function, similar to the example below so I can easily replicate this function in every class (without altering class inheritance).
// added async
static async create(...args: ConstructorParameters<typeof this >){ // error here
await loadSameResources()
return new this(...args)
}