我想引用一个类的类型,该类扩展了一个抽象类并具有一个在其抽象父类中实现的静态工厂函数。
我在这里学习了如何在抽象类中编写静态工厂:https://stackoverflow.com/a/65717206/923846。
请考虑此代码:
abstract class Parent {
public static factory<T extends Parent>(this: new (...args: any[]) => T) {
return new this();
}
}
class Child1 extends Parent { }
class Child2 extends Parent { }
// what specific type can i use instead of "any" here:
const arr: any[] = [ Child1, Child2 ];
let child1 = arr[0].factory();
let child2 = arr[1].factory();
我在 any
中使用了元素的 arr
类型。
我想使用特定类型。
我试图这样声明:
type TParent = typeof Parent;
...
const arr: TParent[] = [ Child1, Child2 ];
...
// i get an error for this line:
let child1 = arr[0].factory();
我收到错误“无法将抽象构造函数类型分配给非抽象构造函数类型”。
那么如何声明这个类型呢?
答案 0 :(得分:2)
在这种情况下,最简单的方法是让 typescript 推断类型并使用 as const
(playground):
const arr = [ Child1, Child2 ] as const;
let child1 = arr[0].factory(); // type Child1
let child2 = arr[1].factory(); // type Child2
对于泛型类型,我们需要去掉 abstract constructor
类型并将其更改为普通类型,以便能够通过 new this()
(playground) 进行实例化:>
// convert the abstract constructor to a normal one and add the static functions via typeof Parent
type ParentSubClass = {new() : Parent} & (typeof Parent);
const arr: ParentSubClass[] = [ Child1, Child2 ];
let child1 = arr[0].factory(); // type Parent
let child2 = arr[1].factory(); // type Parent