0

I have a functional component like

function ItemList({ items }: ItemProps[]) {
  return <p>items[0].name</p>
}

and I'm creating it like:

<ItemList items={items} />

items is an array of objects like [{name: 'a' id:0}, {name: 'b' id:1}].

Everything is working, but in ItemList.jsx TypeScript is telling me Property 'items' does not exist on type 'ItemProps[]'

4
  • 2
    items doesn't exist on ItemProps[], because that's an array. It doesn't even exist on the things in the array. Also the props argument is always an object, not an array. Did you mean { items: ItemProps[] }? Commented May 3, 2022 at 20:05
  • can you show us what is the ItemProps type? Commented May 3, 2022 at 20:05
  • The props object can't be Something[], because it is always an object, and never an array. Arrays can be passed within the props object. I think what you should do is { items }: { items: ItemProps[] }. Commented May 3, 2022 at 20:05
  • 1
    You are destructuring. Without that, you'd write function ItemList(props: { items: ItemProps[] }) With destructuring, you'll still need the items: key. Commented May 3, 2022 at 20:08

1 Answer 1

3
function ItemList({ items }: ItemProps[]) {

This type doesn't mean that items is an array, it means the props object is an array. You need:

function ItemList({ items }: { items: ItemProps[] }) {
Sign up to request clarification or add additional context in comments.

Comments

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.