[英]error TS2345: Argument of type 'X' is not assignable to parameter of type 'X[]'
Currently, there is a strange behaviour when trying to create a new object in Typescript. 当前,尝试在Typescript中创建新对象时出现奇怪的行为。 There are two classes:
有两类:
export class SkaterDrawn {
private ID;
private name;
...
constructor(input?:any) {
this.ID = input.ID;
...
}
export class SkaterDrawnTeam extends SkaterDrawn {
private skaters:SkaterDrawn[];
constructor(skaters:SkaterDrawn[]) {
super(skaters[0]);
this.skaters = skaters;
}
...
}
this.registeredSkaters
contains an array of elements of type SkaterDrawn
. this.registeredSkaters
包含类型的元件的阵列SkaterDrawn
。 Now I'm grouping this array into an array of chunks, like this: 现在,我将该数组分组为一个大块数组,如下所示:
this.registeredSkaters = this.registeredSkaters.groupBy('teamID');
this.registeredSkaters = Object.values(this.registeredSkaters);
This outputs: 输出:
[
[SkaterDrawn,SkaterDrawn,SkaterDrawn],
[SkaterDrawn,SkaterDrawn,SkaterDrawn],
[SkaterDrawn,SkaterDrawn,SkaterDrawn]
]
Now I'm trying to create a new Object out of this: 现在,我试图以此创建一个新的对象:
let teams = this.registeredSkaters.map((item:SkaterDrawn[]) =>
new SkaterDrawnTeam(item);
})
I expected to get an array of SkaterDrawnTeam
, but the compilation fails with 我期望得到一个
SkaterDrawnTeam
数组,但是编译失败并显示
ERROR in src/app/competition/components/drawing/drawing.component.ts(166,30):
error TS2345: Argument of type 'SkaterDrawn' is not assignable to parameter of type 'SkaterDrawn[]'.
Type 'SkaterDrawn' is missing the following properties from type 'SkaterDrawn[]': length, pop, push, concat, and 35 more.
Line 166,30
is the line where I try to do new SkaterDrawnTeam(item)
I don't know why this fails. Line 166,30
行是我尝试做new SkaterDrawnTeam(item)
的行,我不知道为什么会失败。 What am I doing wrong and how can I fix it? 我在做什么错,我该如何解决?
Thx so much for your help. 非常感谢您的帮助。
The problem was the SkaterDrawn[] is not same as (typeof SkaterDrawn)[] 问题是SkaterDrawn []与(typeof SkaterDrawn)[]不同
let teams = this.registeredSkaters.map((item: (typeof SkaterDrawn)[]) => {
return new SkaterDrawnTeam(item);
});
And then change your class as 然后将您的班级更改为
export class SkaterDrawnTeam extends SkaterDrawn {
private skaters:(typeof SkaterDrawn)[];
constructor(skaters:(typeof SkaterDrawn)[]) {
super(skaters);
this.skaters = skaters;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.