Home > database >  Why don't I get all my properties when I use the & operator in TS?
Why don't I get all my properties when I use the & operator in TS?

Time:04-20

I have an interface like this:

export interface ISizeAndColor {
  color: string;
  size: string;
  amount: string;
}

I want to add another object to it, the product_id. So when I make this:

selectedOptions: ISizeAndColor & { product_id: string; }[]

then I only have access to my product_id not the other properties. (When I map it, it is an array.) What I am doing wrong?

CodePudding user response:

The brackets [] have a higher operator priority than the union operator &. Therefore if you want to have an array of your union type you'll have to put it in braces:

selectedOptions: (ISizeAndColor & { product_id: string; })[];
  • Related