Pismotality
Pismotality

Reputation: 2289

How to declare complex array as property of another property Typescript

I am fairly new to Angular and Typescript, and I am trying to declare a property which has a complex array as one of its properties. My attempt looks like this:

groupedItem: {
     customGroupId: string,
     cgName: string,
     category: [{ 
    customGroupItemId: string,
     name: string }]
 };

When I try to access the property

this.groupedItem.category.name

I get an error which states

Property 'name' does not exist on type '[{ customGroupItemId: string; name: string; }]'

It looks like it is there. What am I doing wrong?

Upvotes: 1

Views: 100

Answers (1)

Melchia
Melchia

Reputation: 24234

If you observe your object groupedItem closely you'll see that category is in fact an array not an object, so you need to the following to access it:

this.groupedItem.category[0].name

Upvotes: 1

Related Questions