Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.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
Angular 泛型类中数组属性的实例?_Angular_Typescript_Generics - Fatal编程技术网

Angular 泛型类中数组属性的实例?

Angular 泛型类中数组属性的实例?,angular,typescript,generics,Angular,Typescript,Generics,我有一个typescript的泛型类,它有两个属性- export class WrapperModel<T>{ constructor(private testType: new () => T) { this.getNew(); } getNew(): T { return new this.testType(); } public Entity: T; public ShowLoading:

我有一个typescript的泛型类,它有两个属性-

export class WrapperModel<T>{
    constructor(private testType: new () => T) {
        this.getNew();
    }

    getNew(): T {
        return new this.testType();
    }
    public Entity: T;
    public ShowLoading: boolean;
}
我在上述案例中得到的错误-

是因为我无法在泛型或其他类型中创建数组属性的实例。
我的需要很简单;我想在泛型类中创建
Array
属性的实例。

问题是在运行时泛型会被擦除,因此
Array
不是真正的构造函数,
Array
是构造函数,因此您可以编写:

var userModel = new WrapperModel<Array<UserProfileModel>>(Array);
var userModel=newwrappermodel(数组);
这适用于任何泛型类型,而不仅仅是数组:

class Generic<T> {  }
var other = new WrapperModel<Generic<UserProfileModel>>(Generic);
类泛型{}
var other=新包装器模型(通用);
一般来说,对于泛型类,似乎没有办法获取特定类型实例化的构造函数,只有泛型构造函数:

// Valid, new get a generic constrcutor
var genericCtor: new <T>() => Generic<T> = Generic;

// Not valid,  Generic<UserProfileModel> is not callable
var genericCtor: new () => Generic<UserProfileModel> = Generic<UserProfileModel>;
//有效,新建获取泛型构造函数
var generictor:new()=>Generic=Generic;
//无效,泛型不可调用
var generictor:new()=>Generic=Generic;

成功了,谢谢。它是否只传递所有泛型类型的
typeof
参数?抱歉,我不确定我是否理解这个问题。我想说的是,如果我只传递类型参数-
this.userModel=new WrapperModel(object)
你必须传递不带泛型参数的类,这并不意味着任何泛型类也会,typescript仍在检查兼容性,因此这将给出一个错误:
class-Generic{public-foo:number}class-Generic2{public-foo2:number}var-other=new-WrapperModel(Generic2)
class Generic<T> {  }
var other = new WrapperModel<Generic<UserProfileModel>>(Generic);
// Valid, new get a generic constrcutor
var genericCtor: new <T>() => Generic<T> = Generic;

// Not valid,  Generic<UserProfileModel> is not callable
var genericCtor: new () => Generic<UserProfileModel> = Generic<UserProfileModel>;