Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.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 - Fatal编程技术网

Typescript:使用扩展数组时初始化空数组<;T>;

Typescript:使用扩展数组时初始化空数组<;T>;,typescript,Typescript,我正在尝试添加一个方法,该方法将属于使用Typescript(3.7.5)扩展device数组的类 导出类设备扩展阵列{ 构造函数(项?:数组){ 超级(…项目); } getDeviceByUid(uid:编号):设备{ 返回这个.filter(elmt=>elmt.uid==uid)[0]; } } 但我还需要能够声明和定义空数组,就像我对普通数组所做的那样: unfilteredDeviceList:devices=[]//错误:类型“undefined[]”中缺少属性“getDevic

我正在尝试添加一个方法,该方法将属于使用Typescript(3.7.5)扩展
device
数组的类

导出类设备扩展阵列{
构造函数(项?:数组){
超级(…项目);
}
getDeviceByUid(uid:编号):设备{
返回这个.filter(elmt=>elmt.uid==uid)[0];
}
}
但我还需要能够声明和定义空数组,就像我对普通数组所做的那样:

unfilteredDeviceList:devices=[]//错误:类型“undefined[]”中缺少属性“getDeviceByUid”,但类型“devices”中需要属性“getDeviceByUid”。

所以我试着这样做:
unfilteredDeviceList:devices=新设备()

在执行最新操作时,我没有收到编译时错误,但在运行时:
Error:Uncaught(承诺中):TypeError:super不可编辑(无法读取属性符号(Symbol.iterator))


声明和定义扩展空数组的方法是什么,或者我应该在构造函数中更改什么?

猜测您现在通过
const-arr:devices=new devices([])进行初始化。
猜测您现在通过
const-arr:devices=new devices([])
以这种方式更改构造函数实现应该有效:

class devices extends Array<device> {
    constructor(items?:Array<device>) {
        super();

        if (items) {
            this.push(...items);
        }
    }

    getDeviceByUid(uid: number): device {
        return this.filter(elmt => elmt.uid == uid)[0];
    }
}
类设备扩展数组{
构造函数(项?:数组){
超级();
若有(项目){
这个。推(…项);
}
}
getDeviceByUid(uid:编号):设备{
返回这个.filter(elmt=>elmt.uid==uid)[0];
}
}

以这种方式更改构造函数实现应该可以:

class devices extends Array<device> {
    constructor(items?:Array<device>) {
        super();

        if (items) {
            this.push(...items);
        }
    }

    getDeviceByUid(uid: number): device {
        return this.filter(elmt => elmt.uid == uid)[0];
    }
}
类设备扩展数组{
构造函数(项?:数组){
超级();
若有(项目){
这个。推(…项);
}
}
getDeviceByUid(uid:编号):设备{
返回这个.filter(elmt=>elmt.uid==uid)[0];
}
}

谢谢,它可以工作,但是现在我在Angular中使用它时得到了
TypeError:Found non callable@@iterator
。似乎Angular在执行某些刷新时会将一个
数字传递给构造函数。。。我不明白为什么会这样,但现在我在Angular中使用它时得到了
TypeError:Found non callable@@iterator
。似乎Angular在执行某些刷新时会将一个
数字传递给构造函数。。。我不明白为什么