i have many class already written need translate to async constructor
one sample
class ClassA {
constructor(a: number, b: string, c: string) {
//...
}
//...
}
what i do is add a 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 work’s fine, users just need change new ClassA(...)
to (await ClassA.create(...))
,it’s simple and keep the modify small.
my problem is how to avoid write ‘ClassA’ in create function ,something like bellow so i can copy this function in every class (i can’t modify class inheritance)
// added async
static async create(...args: ConstructorParameters<typeof this>){ // error here
await loadSameResources()
return new this(...args)
}