简体   繁体   English

错误TS2345:类型'X'的参数不能分配给类型'X []'的参数

[英]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;
    }
}

https://stackblitz.com https://stackblitz.com

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我收到错误 TS2345:X 类型的参数不可分配给 Y 类型的参数 - I have got error TS2345: Argument of type X is not assignable to parameter of type Y 错误TS2345:“菜单”类型的参数无法分配给类型参数 - error TS2345: Argument of type 'Menu' is not assignable to parameter of type 错误TS2345:“对象”类型的参数无法分配给类型的参数 - Error TS2345: Argument of type 'Object' is not assignable to parameter of type TS2345类型的参数…无法分配给类型的参数 - TS2345 Argument of type … Is not assignable to parameter of type TS2345:“HTMLElement”类型的参数不可分配给“ChartItem”类型的参数 - TS2345: Argument of type 'HTMLElement' is not assignable to parameter of type 'ChartItem' TS2345:“RolesGuard”类型的参数不可分配给“CanActivate”类型的参数 - TS2345: Argument of type 'RolesGuard' is not assignable to parameter of type 'CanActivate' TS2345:“事件”类型的参数不可分配给“HtmlInputEvent”类型的参数 - TS2345: Argument of type 'Event' is not assignable to parameter of type 'HtmlInputEvent' TS2345:“字符串”类型的参数不可分配给“从不”类型的参数 - TS2345: Argument of type 'string' is not assignable to parameter of type 'never' 错误 TS2345:“事件”类型的参数不可分配给“IPokemon”类型的参数 - error TS2345: Argument of type 'Event' is not assignable to parameter of type 'IPokemon' Angular 错误 TS2345:“MonoTypeOperatorFunction”类型的参数<event> ' 不可分配给 'OperatorFunction 类型的参数<event, event> '</event,></event> - Angular Error TS2345: Argument of type 'MonoTypeOperatorFunction<Event>' is not assignable to parameter of type 'OperatorFunction<Event, Event>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM