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.

V5: Custom selectProps require ts-ignore in Typescript

See original GitHub issue

Hi, congrats on the big v5 release!

After upgrading to v5, use of custom props on Select cause errors in Typescript. They still seem to work but require ts-ignore directive to silence the error.

Example:

    <Select<MyOption>
      id={id}
      options={options}
     {... more props ...}
      components={{ SingleValue }}
      myCustomProp='foobar'
    />

function SingleValue({ children, ...props }: SingleValueProps<MyOption>) {
  return (
      <div>
        <label htmlFor={selectProps.name}>
          {props.selectProps.myCustomProp}
        </label>
         <components.SingleValue {...props}>{children}</components.SingleValue>
         <div className="ml-1">
            <components.DownChevron />
         </div>
      </div>)
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

18reactions
Methuselah96commented, Sep 26, 2021

Sorry, I forgot to include how to type custom selectProps in the upgrade guide and TypeScript usage documentation. I’ll work on adding that documentation.

The only reason this worked for @types/react-select is because there was an index signature that would type any unknown prop as any. This was a little bit too loose and was causing numerous problems. In order to do this in v5 you’ll need to use module augmentation to augment the type of the Select props. Here’s how to update your example so it works in v5:

import Select, { components, GroupBase, SingleValueProps } from 'react-select';

declare module 'react-select/dist/declarations/src/Select' {
  export interface Props<
    Option,
    IsMulti extends boolean,
    Group extends GroupBase<Option>
  > {
    myCustomProp: string;
  }
}

function SingleValue({
  children,
  ...props
}: SingleValueProps<MyOption, false>) {
  return (
    <div>
      <label htmlFor={props.selectProps.name}>
        {props.selectProps.myCustomProp}
      </label>
      <components.SingleValue {...props}>{children}</components.SingleValue>
      <div className="ml-1">
        <components.DownChevron />
      </div>
    </div>
  );
}

export default function App() {
  return (
    <Select<MyOption>
      id={id}
      options={options}
      components={{ SingleValue }}
      myCustomProp="foobar"
    />
  );
}

Let me know if that works for you.

6reactions
Methuselah96commented, Oct 18, 2021

No, unfortunately there’s no way to specify it per component. Note there also wasn’t a way to do this for @types/react-select either. If you want to go back to that behavior you can just augment the props with an index signature that allows any prop.

I’ve looked into trying to make it so that the custom props could be another generic on the Select component, but it made the types much more complex and I think I had trouble getting it working well. Feel free to give it a try yourself and we’d be happy to merge a PR if it doesn’t add too much complexity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use `@ts-ignore` for a block? - Stack Overflow
As a workaround you can use a // @ts-nocheck comment at the top of a file to disable type-checking for that file: ...
Read more >
Styles - React Select
The recommended way to provide custom styles to react-select is to use the styles prop. styles takes an object with keys to represent...
Read more >
react-select - npm
TypeScript. The v5 release represents a rewrite from JavaScript to TypeScript. The types for v4 and earlier releases are available at @types.
Read more >
material-ui/core/CHANGELOG.md - UNPKG
219, - Simplify the theme overrides with TypeScript for the components in the lab (#21279) ... 1290, - [docs] Remove `@ts-ignore` usage (#19504)...
Read more >
ban-ts-comment - TypeScript ESLint
Disallow `@ts- ` comments or require descriptions after directives. ... ts-expect-error , ts-ignore , ts-nocheck , ts-check directives​.
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