Home  >  Q&A  >  body text

React TS error: Missing properties when using Types

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粉539055526P粉539055526220 days ago382

reply all(1)I'll reply

  • P粉904450959

    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 Array
        gender: Gender[]; // or Array
    }

    It's best to name your properties brands and ngenders .

    reply
    0
  • Cancelreply