Typescript/Angular: Object is not Assignable to type

Rafael Picolo

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. 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: enter image description here

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; series: { name: string; value: number; }[]; }[]' is not assignable to type '[{ name: String; series: [{ name: String; value: number; }]; }]'. Target requires 1 element(s) but source may have fewer.

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. Thank you in advance for helping.

Passionate Coder

You have wrongly added type to ibovespaGraphData it is considered as value in your code and type infered different. 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;
  }[]
}[]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Type 'void' is not assignable to object

Type not assignable with object in TS

Type object is not assignable to type any[]

Type 'Observable<Object>' is not assignable to type 'Observable<IUser[]>'

Type 'DefaultClient<unknown>' is not assignable to type 'ApolloClient<object>'

Typescript - object of specified type is not assignable to generic type

"Type 'Object' is not assignable to type" with new HttpClient / HttpGetModule

Typescript error: Type Observable<Object> is not assignable to type

Type 'Observable<Object>' is not assignable to type 'Observable<boolean>'

"type is not assignable" when trying to spread an object

Typescript type Never[] not assignable to object array

'is not assignable to parameter of type' error with object literal

TypeScript: why is a number assignable to a reference of type Object?

'Observable<Object>' is not assignable to type 'Observable<{ user: any; }>'

Type 'Observable<Observable<Object[]>>' is not assignable to type 'Observable<Object[]>'

Error TS2322: Type 'Object[]' is not assignable to type '[Object]'

Type 'undefined' is not assignable to type 'string' when destructing an object

Error TS2322: Type 'Object' is not assignable to type 'Produit'

Typescript error: Argument of type 'Object' is not assignable to parameter of type '{}[]'

Type '{}' is not assignable to type 'object[]' during map and reduce in typescript

Type 'Object' is not assignable to type 'any[]' in Angular 4.x app

Argument of type 'Observable<unknown>' is not assignable to parameter of type 'OperatorFunction<Object, unknown>'

error TS2322: Type 'Object' is not assignable to type 'Contact'. Angular

Angular2 - Type 'object' is not assignable to type 'enum'

Argument of type 'Object' is not assignable to parameter of type 'JSON' Httpclient GET

TS2322: Type 'object' is not assignable to type 'Store'

Type 'object' is not assignable to type 'NgIterable<any> | null | undefined'

“Type 'Object' is not assignable to type” with new HttpClient / HttpGetModule in Angular

Argument of type 'Object' is not assignable to parameter of type 'Blob | MediaSource'