I'm trying to make a filtering system and I'm running into a problem. I set up several types:
type Brand = { brand:string; checked:boolean; } type Gender = { gender: "Male" | "Female" | "Kids"; checked:boolean; } type Shoes = { brand:Brand; gender:Gender; }
Then I try to create a useState object like this:
const [filterState,setFilterState] = useState<Shoes>({ brand:[ { brand:'New Balance', checked: false, }, { brand:'Nike', checked: false, }, { brand:'Addiddas', checked: false, }, { brand:'Puma', checked: false, }, { brand:'Converse', checked: false, }, { brand:'Under Armour', checked: false, }, ], gender:[{ gender:'Male', checked:false, }, { gender:'Female', checked:false, }, { gender:'Kids', checked:false, } ], });
However, on key brands and genders, I'm getting errors like this:
Type '{ brand: string; checked: false; }[]' is missing the following properties from type 'Brand': brand, checked
Even if I add all type attributes. Any help would be greatly appreciated.
P粉9044509592024-04-05 12:25:18
You need to specify that your brand
attribute and gender
attribute are arrays:
type Shoes = { brand: Brand[]; // or Arraygender: Gender[]; // or Array }
It's best to name your properties brands
and ngenders
.