Leff
Leff

Reputation: 1298

Typescript - Property does not exist on type 'xxx[]'

I have an array partialClassList that consists of objects that look like this:

{
  resultsPerTopic: [
    {topic: "Some topic", total: 1, points: 0}
  ],
  code: "B"
}

I have declared the interface for that array like this:

interface IPartialClass {
  code: string,
  resultsPerTopic: ITopic[]
}

interface ITopic {
  topic: string,
  correct: number,
  total: number,
  points: number,
}

And in my code I am trying to map and go through the partialClassList array like this:

result.partialClassList.resultsPerTopic.map((topic, index)

The interface for the result looks like this:

interface IResultModel {
  data: IKeyValue[],
  topic: ITopic[],
  combo: boolean,
  partialClassList: IPartialClass[]
}

But, I get the error:

Property 'resultsPerTopic' does not exist on type 'IPartialClass[]

What am I doing wrong here?

Upvotes: 0

Views: 448

Answers (2)

Eino Gourdin
Eino Gourdin

Reputation: 4507

With result.partialClassList.resultsPerTopic.map((topic, index), you would go through the resultsPerTopic list. But partialClassList is an array and doesn't have a resultsPerTopic attribute.

You must first go through the partialClassList : result.partialClassList.map(p => ...

Upvotes: 1

gztomas
gztomas

Reputation: 3260

partialClassList is an array, you need to provide an index

result.partialClassList[i].resultsPerTopic.map((topic, index)

Upvotes: 0

Related Questions