skip to Main Content

I have this prop that I’ll be passing down:

checkboxesState

its original value is an empty object

{}

but it will change when user clicks on checkboxes, so it can look like this:

{
 "option1": true,
 "option2": true,
 "option3": true,
 "option4": true,
 ...
 ...
}

My question is, How can I define its type in the interface?

I have this but I have a feeling it’s missing something:

interface FieldProps {
   checkboxState: { [key: string]: boolean };
}

2

Answers


  1. Maybe try

    Record<string,boolean>
    

    This means that all keys must be a string and all values must be a boolean

    Login or Signup to reply.
  2. I guess it is what you want

    type Option = {
      [option: `option${number}`]: boolean;
    }
    
    const obj: Option = {
      option1: true,
      option2: true,
      option3: true,
      option44: true,
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search