Extending generic Option with constraints #5201
Answered
by
Methuselah96
VillePakarinenPosti
asked this question in
Q&A
-
Hi, Posting a question here regarding adding constraints on the generic import Select, * as ReactSelect from 'react-select'
// My custom dropdown constraint
export interface DropdownOption extends Record<string, unknown> {
readonly optionLabel: string;
}
export function Dropdown<
Option extends DropdownOption, // Declaring that option should extend DropdownOption
IsMulti extends boolean = false,
Group extends R.GroupBase<Option> = R.GroupBase<Option>
>(props: R.Props<Option, IsMulti, Group>) {
return <Select {...props} components={{
MultiValueContainer: CustomMultiValueContainer,
}} />;
} As you can see I've specified Custom import * as ReactSelect from 'react-select'
function CustomMultiValueContainer<
Option extends DropdownOption,
IsMulti extends boolean = false,
Group extends R.GroupBase<Option> = R.GroupBase<Option>
>(props: R.MultiValueGenericProps<Option, IsMulti,Group>) {
// The type for selectProps.value is PropsValue<Option> = MultiValue<Option> | SingleValue<Option>;
if(Array.isArray(props.selectProps.value)) {
props.selectProps.value.forEach(v => {
// v is of type any, I would expect it to be {optionLabel: string; [key:string]: unknown}
v
})
}
// ..... rest of the component
} So back to my question. Is this the correct pattern for adding constraints to the generic Thank you in advance! |
Beta Was this translation helpful? Give feedback.
Answered by
Methuselah96
May 25, 2022
Replies: 1 comment
-
You're running into microsoft/TypeScript#17002. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
VillePakarinenPosti
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You're running into microsoft/TypeScript#17002.