Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Generics 连接通用运算符<;T>;使用函数构造函数_Generics_Typescript - Fatal编程技术网

Generics 连接通用运算符<;T>;使用函数构造函数

Generics 连接通用运算符<;T>;使用函数构造函数,generics,typescript,Generics,Typescript,我有TS函数,它将JSON反序列化为某种类型/对象。您给它一个类型为的构造函数,它尝试从JSON重构该类型 看起来像这样 export function deserializeJSON<T>(JSONString: string, type?: FunctionConstructor, ... 编辑 我刚刚发现,FunctionConstructor意味着函数的构造函数,所以最好将参数type标记为type:Function,因为所有构造函数都是函数,但是如果不将type转换为,我

我有TS函数,它将JSON反序列化为某种类型/对象。您给它一个类型为的构造函数,它尝试从JSON重构该类型

看起来像这样

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;
}