[英]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.