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: Select typings are incorrect for options parameter

See original GitHub issue

After 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

image

(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

  1. create-react-app my-app --scripts-version=react-scripts-ts
  2. yarn add grommet styled-components
  3. Manually fix the incorrect typings from #2538 in node_modules/grommet/components/Select/index.d.ts
  4. Attempt to use the Select component, passing an array of strings to the options parameter (see example above)
  5. 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jasnrosscommented, Jan 2, 2019

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 an arrayOf, so that it wraps the list of types in Array<> rather than appending a [].

Here is what that could look line in react-desc: https://github.com/jasnross/react-desc/commit/0c93e6190b0f5f5016df90f17427dd113b318e54

1reaction
jasnrosscommented, Jan 1, 2019

I 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:

PropTypes.arrayOf(
  PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.element,
    PropTypes.object
  ])
)

to:

PropTypes.oneOfType([
  PropTypes.arrayOf(PropTypes.string),
  PropTypes.arrayOf(PropTypes.element),
  PropTypes.arrayOf(PropTypes.object)
])

This way react-desc understands that each possible value is itself an array of a single type.

Read more comments on GitHub >

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

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