2

I have the following JSON:

{
  "items": [
    {
      "id": "123",
      "title": "potatoes",
      "category": "Fruit & Veg"
    },
    {
      "id": "456",
      "title": "custard",
      "category": "Other"
    }
  ]
}

...and the following interface:

export interface ShoppingItems {
  category: string;
  id: number;
  title: string;
}

When I import the JSON it works but I am rightly warned that Property 'items' does not exist on type 'ShoppingItems[]'.

Can I take account of the existence of the items array in the interface or does it need to be accounted for outside of the interface?

1
  • Please add the code with the error message. Your JSON isn't a ShoppingItems[]. Commented Nov 12, 2019 at 11:59

1 Answer 1

3

Introduce child interface for a single item

interface ShoppingItem {
  category: string;
  id: number;
  title: string;
}

export interface ShoppingItems {
   items: ShoppingItem[];
}
Sign up to request clarification or add additional context in comments.

1 Comment

many thanks for that. I was guessing it would be something like that but could not get the syntax right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.