-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
V5: Custom selectProps require ts-ignore in Typescript #4804
Comments
Sorry, I forgot to include how to type custom The only reason this worked for 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. |
@Methuselah96 Works for me! Thanks for the response :) |
Is there any way to make this work with multiple components? Thanks in advance. |
No, unfortunately there's no way to specify it per component. Note there also wasn't a way to do this for I've looked into trying to make it so that the custom props could be another generic on the |
I tried adding the |
No, not off-hand. Are you sure |
Yes, importing Seems that there was a conflict with another |
Is this in the docs yet? I was looking for it but couldn't find it. Glad I was able to find this here! It would be nice to be able to reference docs for the rest of my team that may come across this in the future vs. an issue here. |
Hi @Methuselah96 , I have different use cases for react-select within my app and when I use module augmentation the result is that now every instance of react-select is being required to comply to the new augmented props. Is there a way to work around this? |
If you are using an external file for defining the typings, use this: Inside
|
This just bit me, too. Although it's kind of obvious, the import could be included in the example in the docs for copy-pasters like me, since even eslint didn't seem to notice it 😉 |
I managed to augment /* eslint-disable @typescript-eslint/no-unused-vars */
import { Props } from 'react-select/dist/declarations/src/Select';
/**
* @see https://react-select.com/typescript#custom-select-props
*/
declare module 'react-select/dist/declarations/src/Select' {
import { GroupBase } from 'react-select';
export interface Props<
Option,
IsMulti extends boolean,
Group extends GroupBase<Option>
> {
/**
* `true` if value is valid.
*/
isValid?: boolean;
}
} |
The docs for this issue appear to have changed, and since updating react-select and using what is shown in the docs in my component library, consumers of my library are no longer getting my custom props, although they are available within the library. Previously, consumers were able to pick up these custom prop types fine. In my Select component's index in the library: import type {} from 'react-select/base';
// https://react-select.com/typescript#custom-select-props
declare module 'react-select/base' {
export interface Props<
Option,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
IsMulti extends boolean,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Group extends GroupBase<Option>
> {
appearance?: SelectAppearance;
isSortable?: boolean;
status?: InputValidationStatus;
maxSelectedOptions?: number;
}
} Edit: after experimenting with npm link locally, I was able to fix the issue by reverting to the previous declaration path, while keeping the 5.8 version: declare module 'react-select/dist/declarations/src/Select' {
export interface Props<
Option,
// Must include these generics, but they are unused in the added custom props
// eslint-disable-next-line @typescript-eslint/no-unused-vars
IsMulti extends boolean = false,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Group extends GroupBase<Option> = GroupBase<Option>
> {
appearance?: SelectAppearance;
isSortable?: boolean;
status?: InputValidationStatus;
maxSelectedOptions?: number;
}
} Is it possible there is something about the new recommended module path in the docs that does not get picked up by library consumers properly? |
I found a very hacky, unsound workaround for now: const Group: React.FC<GroupProps<MyCustomOptions>> = (props) => {
const {
selectProps,
} = props;
const {
myCustomProp,
} = (selectProps as unknown as {
myCustomProp: ...
});
return <CustomComponent myCustomProp={myCustomProp} />
};
///// later, to use it
<Select
// Pass-along props to CustomComponent
// Must use this spread-of-lieral-object shenanigan to
// dodge TS's complaining about unknown properties
{... { myCustomProp: "some value" }}
/> As far as I can tell, the custom props get passed along just fine; I just needed to hide them from the overzealous type-checker here :) |
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:
The text was updated successfully, but these errors were encountered: