i have many class already written need translate to async constructor
one sample
<code>class ClassA {
constructor(a: number, b: string, c: string) {
//...
}
//...
}
</code>
<code>class ClassA {
constructor(a: number, b: string, c: string) {
//...
}
//...
}
</code>
class ClassA {
constructor(a: number, b: string, c: string) {
//...
}
//...
}
what i do is add a async create method
<code>class ClassA {
// added async
static async create(...args: ConstructorParameters<typeof ClassA>){
await loadSameResources()
return new this(...args)
}
constructor(a: number, b: string, c: string) {
//...
}
}
</code>
<code>class ClassA {
// added async
static async create(...args: ConstructorParameters<typeof ClassA>){
await loadSameResources()
return new this(...args)
}
constructor(a: number, b: string, c: string) {
//...
}
}
</code>
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)
<code> // added async
static async create(...args: ConstructorParameters<typeof this>){ // error here
await loadSameResources()
return new this(...args)
}
</code>
<code> // added async
static async create(...args: ConstructorParameters<typeof this>){ // error here
await loadSameResources()
return new this(...args)
}
</code>
// added async
static async create(...args: ConstructorParameters<typeof this>){ // error here
await loadSameResources()
return new this(...args)
}