【发布时间】:2020-09-25 08:38:41
【问题描述】:
我正在尝试在 typescript 中实现一个返回泛型类型的工厂。
已经发现我需要将类型作为第一个参数传递,并将其类型设置为 CTOR 签名(在此示例中为 new () => T)。
当我想将泛型类型传递给工厂时,问题就开始了 - 我收到一条错误消息:
Value of type 'typeof G' is not callable. Did you mean to include 'new'?(2348).
有什么方法可以实现吗?
这是问题的简化版本:
// Generic class
class G<T>{}
// Standard non generic
class B{}
function make<T>(t: new () => T){
return new t()
}
// Works
make(B)
// Value of type 'typeof G' is not callable. Did you mean to include 'new'?(2348)
make(G<number>)
【问题讨论】:
标签: typescript generics factory