Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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
Typescript 参数中T的泛型和类型_Typescript_Typescript1.8 - Fatal编程技术网

Typescript 参数中T的泛型和类型

Typescript 参数中T的泛型和类型,typescript,typescript1.8,Typescript,Typescript1.8,在TypeScript中,我可以将变量的类型定义为类的类型。例如: class MyClass { ... } let myVar: typeof MyClass = MyClass; 现在我想把它用于一个泛型类,比如: class MyManager<T> { constructor(cls: typeof T) { ... } /* some other methods, which uses instances of T */ } let test = n

在TypeScript中,我可以将变量的类型定义为类的类型。例如:

class MyClass { ... }

let myVar: typeof MyClass = MyClass;
现在我想把它用于一个泛型类,比如:

class MyManager<T> {
    constructor(cls: typeof T) { ... }
    /* some other methods, which uses instances of T */
}

let test = new MyManager(MyClass); /* <MyClass> should be implied by the parameter */
类MyManager{
构造函数(cls:typeof T){…}
/*其他一些方法,使用T的实例*/
}
let test=新的MyManager(MyClass);/*应该由参数暗示*/
因此,我想给我的管理器类另一个类(它的构造函数),因为管理器需要检索与该类关联的静态信息

在编译我的代码时,它说它找不到名称“T”,我的构造函数在那里


你知道如何解决它吗?

你可以使用这种类型的构造函数:
{new():ClassType}

class MyManager<T> {
    private cls: { new(): T };

    constructor(cls: { new(): T }) {
        this.cls = cls;
    }

    createInstance(): T {
        return new this.cls();
    }
}

class MyClass {}

let test = new MyManager(MyClass);
let a = test.createInstance();
console.log(a instanceof MyClass); // true
例如,在typescript中:

接口阵列构造函数{
新(排列长度?:编号):任意[];
新(排列长度:编号):T[];
新(…项:T[]):T[];
(arrayLength?:数字):任何[];
(排列长度:编号):T[];
(…项目:T[]):T[];
isArray(arg:any):arg是数组;
只读原型:数组;
}
这里有3个不同的ctor签名和一组静态函数。
在您的情况下,您也可以这样定义它:

interface ClassConstructor<T> {
    new(): T;
}

class MyManager<T> {
    private cls: ClassConstructor<T>;

    constructor(cls: ClassConstructor<T>) {
        this.cls = cls;
    }

    createInstance(): T {
        return new this.cls();
    }
}
接口类构造函数{
新的():T;
}
班级经理{
私有cls:类构造函数;
构造函数(cls:ClassConstructor){
this.cls=cls;
}
createInstance():T{
返回新的this.cls();
}
}

这并不是很明显的
{new():T}
是关于什么的,特别是因为构造函数被定义为
constructor()
,人们更希望看到像
T
typeof T
之类的东西。这是定义类类型/ctor的标准方法,有关更多信息,请查看我的修订答案。@NitzanTomer如何编写构造函数以允许
let test=new MyManager(Array)?或
…MyManager(MyClass[])
@GFoley83:
构造函数(cls:myclassconstructor[])
@NitzanTomer我要找的是
构造函数(cls:{new(…args:any[]):T}[])
interface ArrayConstructor {
    new (arrayLength?: number): any[];
    new <T>(arrayLength: number): T[];
    new <T>(...items: T[]): T[];
    (arrayLength?: number): any[];
    <T>(arrayLength: number): T[];
    <T>(...items: T[]): T[];
    isArray(arg: any): arg is Array<any>;
    readonly prototype: Array<any>;
}
interface ClassConstructor<T> {
    new(): T;
}

class MyManager<T> {
    private cls: ClassConstructor<T>;

    constructor(cls: ClassConstructor<T>) {
        this.cls = cls;
    }

    createInstance(): T {
        return new this.cls();
    }
}