TypeScript: Select typings are incorrect for options parameter
See original GitHub issueAfter working around #2538 using patch-package I ran into further issues with the Select typings not accepting an array of string as options
:
Expected Behavior
When using TypeScript, the Select
component should accept an array of strings as a valid value for the options
property.
import { Select } from 'grommet';
import * as React from 'react';
const options = ['first', 'second', 'third'];
export const ExampleComponent: React.FunctionComponent = () => (
<Select value="first" options={options} />
);
Actual Behavior
TypeScript complains about string
not being assignable to object
Type 'string[]' is not assignable to type 'string | Element | object[]'.
Type 'string[]' is not assignable to type 'object[]'.
Type 'string' is not assignable to type 'object'.
This is due to the following typing:
options: string | JSX.Element | object[];
Which I believe instead should be:
options: string[] | JSX.Element[] | object[];
URL, screen shot, or Codepen exhibiting the issue
(I attempted to do this in CodeSandbox but couldn’t seem to get it working with TypeScript. Happy to try another way if this doesn’t suffice!)
Steps to Reproduce
- create-react-app my-app --scripts-version=react-scripts-ts
yarn add grommet styled-components
- Manually fix the incorrect typings from #2538 in
node_modules/grommet/components/Select/index.d.ts
- Attempt to use the Select component, passing an array of strings to the
options
parameter (see example above) yarn run build
Your Environment
- Grommet version: 2.1.1
- TypeScript Version: 3.2.2
- Browser Name and version: N/A
- Operating System and version (desktop or mobile): MacOS 10.12.6
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top Results From Across the Web
react-select and typescript: Type 'string' is not assignable to ...
You have set the state to the string "chocolate" and it's throwing an error indicating that isn't of the same type. If you...
Read more >Functions - TypeScript: Handbook
As long as the parameter types line up, it's considered a valid type for the function, regardless of the names you give the...
Read more >Documentation - Everyday Types - TypeScript
TypeScript allows you to specify the types of both the input and output values of functions. Parameter Type Annotations. When you declare a...
Read more >Documentation - Narrowing - TypeScript
Argument of type 'string | number' is not assignable to parameter of type ... and then choose their branches depending on whether the...
Read more >Documentation - TypeScript 4.0
Unfortunately, you'd also end up with the same sorts of issues typing a function like ... Improving the experience around tuple types and...
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
Thinking about this more, the other type that is maybe more in the spirit of the existing PropTypes would be:
Array<string | JSX.Element | object>
This way it matches the original PropTypes definition, where the array can be of mixed types.
In that case it would be a fix in how
react-desc
generates the type for anarrayOf
, so that it wraps the list of types inArray<>
rather than appending a[]
.Here is what that could look line in
react-desc
: https://github.com/jasnross/react-desc/commit/0c93e6190b0f5f5016df90f17427dd113b318e54I began working on a fix for this issue but I’m not sure if it’s the right approach.
Would love some feedback and if it seems good I can submit it as a PR:
https://github.com/jasnross/grommet/commit/5bd82c738270eceadebdcafed2bfb0d7cba7ac75
Basically the approach is to change the PropTypes def for
options
from:to:
This way
react-desc
understands that each possible value is itself an array of a single type.