Generics 连接通用运算符<;T>;使用函数构造函数
我有TS函数,它将JSON反序列化为某种类型/对象。您给它一个类型为的构造函数,它尝试从JSON重构该类型 看起来像这样Generics 连接通用运算符<;T>;使用函数构造函数,generics,typescript,Generics,Typescript,我有TS函数,它将JSON反序列化为某种类型/对象。您给它一个类型为的构造函数,它尝试从JSON重构该类型 看起来像这样 export function deserializeJSON<T>(JSONString: string, type?: FunctionConstructor, ... 编辑 我刚刚发现,FunctionConstructor意味着函数的构造函数,所以最好将参数type标记为type:Function,因为所有构造函数都是函数,但是如果不将type转换为,我
export function deserializeJSON<T>(JSONString: string, type?: FunctionConstructor, ...
编辑
我刚刚发现,FunctionConstructor
意味着函数的构造函数,所以最好将参数type
标记为type:Function
,因为所有构造函数都是函数,但是如果不将type转换为
,我就无法实例化新类型。我想表达类似于.constructor
的东西
编辑
我已经定义了接口:
interface Constructor<T> extends Function{
new (): T;
new (...args: Array<any>): T;
(...args: Array<any>): T;
prototype: T;
}
接口构造函数扩展函数{
新的():T;
新(…参数:数组):T;
(…args:Array):T;
原型:T;
}
它基本上是泛型的newable函数
,但是这个类型“…不可分配给DateConstructor类型的parametr”或任何其他类型的构造函数,所以最好还是使用类型:函数
,因为它接受所有构造函数。回答我自己的问题
此接口需要更多测试,但似乎可以正常工作:
interface Constructor<T> extends Function {
new (): T;
new (...args: Array<any>): T;
prototype: T;
}
接口构造函数扩展函数{
新的():T;
新(…参数:数组):T;
原型:T;
}
我想这不可能直接用于这种类型。你如何调用这个函数,你能举个例子吗?
interface Constructor<T> extends Function {
new (): T;
new (...args: Array<any>): T;
prototype: T;
}