question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript for OptionType is not accepting React component

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

5reactions
wegrycommented, May 2, 2022

@Methuselah96 here’s a repro with react-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 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}.

1reaction
Rall3ncommented, Feb 1, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found