Typescript for OptionType is not accepting React component
See original GitHub issueThe 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.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7
Top Results From Across the Web
react-select and typescript: Type 'string' is not assignable to ...
You'll need to define a type for the options and use `ValueType´ in the onChange function signature. Here's a working example type OptionType...
Read more >Static Type Checking - React
Static type checkers like Flow and TypeScript identify certain types of ... It lets you annotate the variables, functions, and React components with...
Read more >API - React Select
A flexible and beautiful Select Input control for ReactJS with multiselect, autocomplete and ajax support.
Read more >React & TypeScript: use generics to improve your types
TypeScript generics in React are very useful, but can be scary when you ... If I were to write it using a JavaScript-like...
Read more >Passing Generics to JSX Elements in TypeScript - Marius Schulz
Type 'number' is not assignable to type 'SetStateAction '. So how do we properly type our React component? The answer is generics. #Step...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Methuselah96 here’s a repro withreact-select@5.3.1
.https://codesandbox.io/s/hopeful-chaum-v385xj and typescript@4.4.2.Just noticed that the
value
prop has to be the same shape asoptions
elements. Ifvalue
’s astring
, thenoptions
must be astring[]
. Ifoptions
is{value: string; label: string}[]
, thenvalue
must be{value: string; label: string}
.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.