Currently, there is a strange behaviour when trying to create a new object in 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
. 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
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. 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)[]
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.