'typeof object of array of object in typescript?
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}
/>
);
};
Solution 1:[1]
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.
Solution 2:[2]
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
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JSON Derulo |
| Solution 2 | Medet Tleukabiluly |
