react-select icon indicating copy to clipboard operation
react-select copied to clipboard

Typescript for OptionType is not accepting React component

Open cristian-sima opened this issue 3 years ago • 10 comments

The code will work as expected, but typescript ruqires me to have labels as strings.

Type '{ value: string; label: Element; }[]' is not assignable to type 'readonly ({ value: string; label: string; } | GroupBase<{ value: string; label: string; }>)[]'.ts(2322)
Select.d.ts(180, 5): The expected type comes from property 'options' which is declared here on type 'IntrinsicAttributes & Omit<Pick<Props<{ value: string; label: string; }, false, GroupBase<{ value: string; label: string; }>>, "value" | "form" | ... 28 more ... | "theme"> & Partial<...> & Partial<...>, StateManagedPropKeys> & Partial<...> & StateManagerAdditionalProps<...> & RefAttributes<...>'
 var options = [ {value: 0, label: () => (<span>{"All"}</span>)}],

     <Select
            ...
                    options={options}
           ...
                  />

You may also find the online Babel tool quite helpful if you wish to use ES6/ES7 syntax not yet supported by the browser you are using.

cristian-sima avatar Jan 31 '22 22:01 cristian-sima

The reason the types don't allow it is because the labels need to be strings so that they can be used for accessibility.

Methuselah96 avatar Feb 01 '22 00:02 Methuselah96

Additionally if you want to format your options based on provided data, you should either style them using the styling framework or use the formatOptionLabel prop to customize the JSX of the label.

Rall3n avatar Feb 01 '22 06:02 Rall3n

Good morning @Methuselah96 , my array has all string data, but I get the same error.

const optYear = [
  { value: '2022', label: '2022' },
  { value: '2021', label: '2021' },
  { value: '2020', label: '2020' },
  { value: '2019', label: '2019' },
  { value: '2018', label: '2018' },
 ] 

KesleyDavid avatar Mar 14 '22 20:03 KesleyDavid

@cristian-sima @KesleyDavid Can one of you please provide a CodeSandbox with your issue? I am not able to reproduce this.

Methuselah96 avatar Apr 18 '22 14:04 Methuselah96

I also have no issue with the latest version

HwangTaehyun avatar Apr 18 '22 14:04 HwangTaehyun

~~@Methuselah96 here's a repro with [email protected].~~

~~https://codesandbox.io/s/hopeful-chaum-v385xj and [email protected].~~

Just noticed that the value prop has to be the same shape as options elements. If value's a string, then options must be a string[]. If options is {value: string; label: string}[], then value must be {value: string; label: string}.

wegry avatar May 02 '22 13:05 wegry

Yes, the value needs to be the same type as one of the options. Last call for reproduction. As far as I can tell, the reports so far have been legitimate type errors.

Methuselah96 avatar May 02 '22 15:05 Methuselah96

This needs to be explained better in documentation (preferably with some examples). It's really unintuitive and TS throws error with options type, not with value type.

VxMxPx avatar Aug 16 '23 15:08 VxMxPx

i had the same issue just had to remove the value prop

yvan99 avatar Mar 12 '24 14:03 yvan99

isMulti={false} solves my problem

<Select instanceId = "from-currencies-box" styles={customStyles} components={{ Option: optimizeSelect.components.Option, MenuList: optimizeSelect.components.MenuList }} options={(fromCurrencies || []).map((currency) => ({
    value: currency.currency_id,
    label: currency.currency_name,
}))} filterOption={createFilter({ ignoreAccents: false })} isMulti={false} onChange={(e) => setFromCurrency(e.value)} />

fzn0x avatar Apr 13 '24 04:04 fzn0x