我正在尝试制作一个过滤系统,但遇到了问题。 我设置了几种类型:
type Brand = { brand:string; checked:boolean; } type Gender = { gender: "Male" | "Female" | "Kids"; checked:boolean; } type Shoes = { brand:Brand; gender:Gender; }
然后我尝试创建一个如下所示的 useState 对象:
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, } ], });
但是,在关键品牌和性别上,我遇到了此类错误:
Type '{ brand: string; checked: false; }[]' is missing the following properties from type 'Brand': brand, checked
即使我添加了所有类型属性。任何帮助将不胜感激。
P粉9044509592024-04-05 12:25:18
您需要指定您的 brand
属性和 gender
属性是数组:
type Shoes = { brand: Brand[]; // or Arraygender: Gender[]; // or Array }
最好将您的属性命名为 brands
和 ngenders
。