skip to Main Content

I’m new to Typescript, converting a react project from js to ts

I checked out some other posts, but I’m going round in circles

any help would be appreciated

my cart object looks like this

{
    "9190970": {
        "name": "Lachlan Ridge Shiraz 2L",
        "brand": "Lachlan Ridge",
        "shortName": "Shiraz 2L",
        "price": 8,
        "qty": 1,
        "deal": {
            "percentOff": 10
        },
        "discountCode": "SAVE10"
    },
    "9197550": {
        "name": "St Hugo GSM 750mL",
        "brand": "St Hugo",
        "shortName": "GSM 750mL",
        "price": 40,
        "qty": 1,
        "deal": 0
    },
    "9948867": {
        "name": "Squealing Pig Marlborough Sauvignon Blanc 750mL",
        "brand": "Squealing Pig",
        "shortName": "Marlborough Sauvignon Blanc 750mL",
        "price": 16,
        "qty": 2,
        "deal": 0
    }
}

Then in my Cart Component

type CartProps = {
  [id: string]: {
    name: string;
    brand: string;
    shortName: string;
    price: number;
    qty: number;
    deal?: {
      twoFor?: number;
    };
    dealPrice?: number;
    discountCode?: string;
  };
};

const cart: CartProps = useSelector(selectCart); // redux

render fn

  <ul className={styles.list}>
    {Object.values(cart).map(
      ([id, { name, brand, shortName, qty, price, deal, dealPrice }]) => (
       // ERROR HERE: must have a '[Symbol.iterator]()' method that returns an iterator.ts(2488)

Thanks

2

Answers


  1. use Object.entries instead of Object.values in your render code

    Login or Signup to reply.
  2. Object.values(cart) will return an array of your object values.

    Looking at ([id, { name, brand, shortName, qty, price, deal, dealPrice }]) you’re expecting pairs of key/value

    Object.entries is what you are looking for.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search