简体   繁体   English

打字稿/角度:Object 不可分配给类型

[英]Typescript/Angular: Object is not Assignable to type

so I'm using ngx-charts for my project and currently I'm stuck in a seemingly dull error on my end of the typescript code.所以我在我的项目中使用 ngx-charts,目前我在 typescript 代码的末尾陷入了一个看似无聊的错误。 I have tested the following data and it is working as expected:我已经测试了以下数据,它按预期工作:

export var productSalesMulti = [
{
  name: 'Cotação',
  series: [
    {
      name: '0',
      value: 10,
    },
    {
      name: '1',
      value: 20,
    },
    {
      name: '2',
      value: 30,
    },
    {
      name: '3',
      value: 20,
    },
    {
      name: '4',
      value: 40,
    },
    {
      name: '5',
      value: 30,
    },
    {
      name: '6',
      value: 10,
    },
    {
      name: '7',
      value: 15,
    },
    {
      name: '8',
      value: 35,
    },
    {
      name: '9',
      value: 50,
    },
    {
      name: '10',
      value: 35,
    },
    {
      name: '11',
      value: 40,
    }
  ]

}]

The input of my component is looking like:我的组件的输入看起来像: 在此处输入图像描述

But I`m getting the following error:但我收到以下错误:

Error: src/app/shared/components/grafico-ibovespa/grafico-ibovespa.component.ts:40:5 - error TS2322: Type '{ name: string;错误:src/app/shared/components/grafico-ibovespa/grafico-ibovespa.component.ts:40:5 - 错误 TS2322:类型 '{ name: string; series: { name: string;系列:{名称:字符串; value: number;值:数字; }[]; }[]; }[]' is not assignable to type '[{ name: String; }[]' 不能分配给类型 '[{ name: String; series: [{ name: String;系列:[{名称:字符串; value: number;值:数字; }]; }]; }]'. }]'。 Target requires 1 element(s) but source may have fewer.目标需要 1 个元素,但源可能有更少。

40 this.ibovespaGraphData = productSalesMulti; 40 this.ibovespaGraphData = productSalesMulti;

So I know that my input declaration is wrong, but I'm seeing where the problem is.所以我知道我的输入声明是错误的,但我看到了问题所在。 I know I could just write: @Input() ibovespaGraphData: any[] and it would work, but for sake of readability I would rather not to.我知道我可以写:@Input() ibovespaGraphData: any[] 它会工作,但为了可读性我宁愿不这样做。 Thank you in advance for helping.预先感谢您的帮助。

You have wrongly added type to ibovespaGraphData it is considered as value in your code and type infered different.您错误地将类型添加到ibovespaGraphData它被视为代码中的值并且类型推断不同。 It should be like below它应该如下所示

ibovespaGraphData : Array<{
  name: string;
  series: Array<{
    name: string;
    value: number;
  }>
}>

or like this或者像这样

ibovespaGraphData  : {
  name: string;
  series: {
    name: string;
    value: number;
  }[]
}[]

Try below code, should work-试试下面的代码,应该可以 -

@Input('dadosIbovespaGrafico') IbovespaGraphData: {
  name: string;
  series: {
    name: string;
    value: number;
  }[]
}[]

暂无
暂无

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

相关问题 角度2/4 | 类型“ {}”不可分配给类型“对象[]” - Angular 2/4 | Type '{}' is not assignable to type 'Object[]' 类型“null”不能分配给类型“string”。 角/打字稿 - Type 'null' is not assignable to type 'string'. Angular/TypeScript Type Headers 不可分配给 TypeScript 和 Angular 9 中的类型 HttpHeaders - Type Headers is not assignable to type HttpHeaders in TypeScript and Angular 9 打字稿错误:可观察类型 <Object> 不可分配给类型 - Typescript error: Type Observable<Object> is not assignable to type 类型响应的 Angular 2 和 typescript 参数不可分配 - Angular 2 and typescript argument of type response is not assignable 打字稿:类型不可分配给类型 - Typescript : Type is not assignable to type 角度/打字稿错误:类型'Observable<unknown> ' 不可分配给类型 'Observable<blogpost> '</blogpost></unknown> - Angular/typescript error: Type 'Observable<unknown>' is not assignable to type 'Observable<Blogpost>' Angular Object 类型的参数不能分配给参数 Never - Angular Argument of Type Object Is Not Assignable to Parameter Never Angular TypeScript错误:无法将类型“ void”分配给类型“ boolean” - Angular TypeScript error: Type 'void' is not assignable to type 'boolean' 键入'{类型代码:字符串; }[]' 不能分配给类型 'string[]'。 // Angular 9.1.15, TypeScript - Type '{ typecode: string; }[]' is not assignable to type 'string[]'. // Angular 9.1.15, TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM