Home > database >  typeof object of array of object in typescript?
typeof object of array of object in typescript?

Time:02-08

How do I replace any below with options's object shape below?

interface selectComponentProps {
  options: {
    label: string;
    value: string;
  }[];
}

const SelectComponent: React.FC<selectComponentProps> = ({
   options,
}) => {
  const handleChange = (selectedOption: any) => { //here
    
  };

  return (
    <Select
      options={options}
    />
  );
};

CodePudding user response:

You can extract the model for a single option to a separate interface:

interface selectComponentProps {
  options: selectComponentPropsOption[];
}

interface selectComponentPropsOption {
  label: string;
  value: string;
}

Then you can replace any with selectComponentPropsOption.

CodePudding user response:

Option 1.

const handleChange = (selectedOption: typeof options[0]) => { //here
    
};

Option 2.

interface Option {
  label: string;
  value: string;
}
interface selectComponentProps {
  options: Option[];
}
const handleChange = (selectedOption: Option) => { //here
    
};
  •  Tags:  
  • Related