Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Property xyz does not exist on type 'IntrinsicAttributes & abc interface

I have an array of objects like:-

const data = {
"Large_Plates": [
    {
      "name": "Cauliower/ Shanghai Fried rice with stir fry vegetables",
      "id": "1",
      "price_Veg": "545",
      "price_Lamb": null,
      "price_Chicken": null,
      "price_SeaFood": null,
      "Desp": "A vegan & gluten free scrumptious and healthy meal"
    },
],
"Salads": [
    {
      "name": "Vietnamese Noodle Salad",
      "id": "1",
      "price_Veg": "439",
      "price_Chicken": "488",
      "price_SeaFood": "549",
      "Desp": "Rice noodles, vegetables, oriental relish"
    }
],
"Tea_Coffe": [
    { "name": "Immunity Tea", "id": "1", "price": "209" }
]
}

For this I have created an interface like :-

interface FoodProps {
    name:string,
    id:string,
    Desp?:string |null,
    price?:string,
    price_Veg?:string |null,
    price_Chicken?:string| null,
    price_SeaFood?:string| null,
    price_Lamb?:string |null
  }

But when I am trying to do (in React with TS)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{data[SelectMenuItem].map((singlefooditem: FoodProps) => {
            return (
              <>
                <FoodItem
                  singlefooditem={singlefooditem}
                  key={singlefooditem.id}
                />
              </>
            );
          })}

and get this on another component :-

const FoodItem = ({ singlefooditem }: FoodProps) => {
  console.log(singlefooditem);
  return (
    <div className="food-item-container">
      <div className="top-food-item">
        <div className="name-fi">{singlefooditem.name}</div>
        <div className="descp-fi"></div>
      </div>
      <div className="bot-food-item">
        <div className="veg-fi"></div>
        <div className="chicken-fi"></div>
        <div className="seafood-fi"></div>
        <div className="lamb-fi"></div>
      </div>
    </div>
  );
};

I am getting an error like :

  1. on FoodItem component – Property ‘singlefooditem’ does not exist on type ‘FoodProps’.ts(2339)

  2. on Parent component – Type ‘{ singlefooditem: FoodProps; key: string; }’ is not assignable to type ‘IntrinsicAttributes & FoodProps’.
    Property ‘singlefooditem’ does not exist on type ‘IntrinsicAttributes & FoodProps’.ts(2322)

Please let me know why is this happening as interface is correct and I am just trying to prop drill!!

>Solution :

Use type props or interface for the FoodItem component.

type FoodItemProps = {
  singlefooditem: FoodProps
}

const FoodItem: FC<FoodItemProps> = ({ singlefooditem }) => {
  console.log(singlefooditem);
  return (
    <div className="food-item-container">
      <div className="top-food-item">
        <div className="name-fi">{singlefooditem.name}</div>
        <div className="descp-fi"></div>
      </div>
      <div className="bot-food-item">
        <div className="veg-fi"></div>
        <div className="chicken-fi"></div>
        <div className="seafood-fi"></div>
        <div className="lamb-fi"></div>
      </div>
    </div>
  );
};

FC is the type of Functional Component, exported from react.

and if you want use one-liner

const FoodItem = ({ singlefooditem }: { singlefooditem: FoodProps }) => {

Thanks

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading