class Context {
}
class MyClass1 {
constructor(context: Context, argv: number) {
//do something with context
}
}
class MyClass2 {
constructor(context: Context, argv1: string, argv2: number) {
// do something with context
}
}
type ExcludeContext<T extends new (context: Context, ...argv: any) => any> =
T extends new (context: Context, ...argv: infer P) => any ? P : never
type a = ExcludeContext<typeof MyClass1> // [number]
type b = ExcludeContext<typeof MyClass2> // [string, number]
class Factory {
constructor(private context: Context) { }
createObj(template: typeof MyClass1, ...argv: ExcludeContext<typeof MyClass1>) {
return new template(this.context, ...argv)
}
createObj2<T extends (typeof MyClass1) | (typeof MyClass2)>(template: T, ...argv: ExcludeContext<T>) {
// Oops,compiler prompt error for argv
// Expected 3 arguments, but got 1 or more.
// An argument for 'argv' was not provided
return new template(this.context, ...argv)
}
}
У меня есть много классов, которые используют contenxt для constructor
.
Однако контекст предназначен для передачи из Factory, так что эти классы должны быть созданы Factory.
Я хочу реализовать простое обобщение c метод для создания ob js. Это нормально для createObj
, но все пошло не так, когда я попытался объявить обобщенный c метод createObj2
.
template
был показан как new (context:Context,argv1:never,argv2:number)=>MyClass1|MyClass2