JedWatson / react-select

The Select Component for React.js
https://react-select.com/
MIT License
27.63k stars 4.13k forks source link

Wrong option type in getOptionLabel/getOptionValue #5569

Closed nik-webdevelop closed 1 year ago

nik-webdevelop commented 1 year ago

When I use Select with groups, the option parameter in getOptionalLabel/getOptionalValue is inferred as a whole group. Function getOptionLabel={(option) => option.name} is accessing the name properly but TS shows errors.

const colors = [
  {
    label: "Colors",
    options: [
      { name: "white", value: "1" },
      { name: "yellow", value: "2" }
    ]
  }
];

<Select
  options={colors}
  getOptionLabel={(option: inferred as group and not the actual option) => ... }
  getOptionValue={(option: inferred as group and not the actual option) => ... }
/>

Codesandbox

Rall3n commented 1 year ago

@nik-webdevelop If the Select component has problems inferring the option type from the given options, you can provide a set type for those options to the component:

<Select<{name: string, value: string}>
  options={colors}
  getOptionLabel={(opt) => opt.name} // properly inferred as option
/>
nik-webdevelop commented 1 year ago

Thanks for your response. Yes, this will work, but it's not really handy to explicitly pass option type each time. I am just surprised that an option isn't inferring by default. P.S. also setting defaultValue or value fixes an option inferring