Combobox multiple prop fails typecheck unless false
What package within Headless UI are you using?
@headlessui/react
What version of that package are you using?
v1.7.16
What browser are you using?
Chrome v115.0.5790.171 (64 bits)
Reproduction URL
https://stackblitz.com/edit/vitejs-vite-np7ad5?file=src%2FApp.tsx
Check error on src/App.tsx line 32
Describe your issue
Can't get <Combobox multiple> to work with Typescript unless it's set to false due to wrong prop type (it says false instead of boolean).
The error:
No overload matches this call.
The last overload gave the following error.
Type 'true' is not assignable to type 'false'.(2769)
combobox.d.ts(39, 5): The expected type comes from property 'multiple' which is declared here on type 'IntrinsicAttributes & { value?: SetStateAction<{ id: number; name: string; }[]> | undefined; nullable?: false | undefined; multiple?: false | undefined; defaultValue?: SetStateAction<...> | undefined; onChange?(value: SetStateAction<...>): void; by?: ByComparator<...> | undefined; } & CleanProps<...> & OurProps<...>...'
combobox.d.ts(111, 5): The last overload is declared here.
Hey! This is a less-than-deal situation around TypeScript's type checking highlighting the wrong thing for an error. We spend a good amount of time on it and were able to make it better but it's still not perfect.
The reason you're seeing the error is caused by two things:
selectedneeds to be of an array type for amultiplecomboboxsetSelected's accepted arguments don't line up exactly (it includes a function) and this causes TypeScript's inference to complain even though it's technically okay. Probably for the same reason thatselectedbeing wrong highlight'smultiplerather thanvalue.
If you make changes to address both of these the error goes away: https://stackblitz.com/edit/vitejs-vite-cpbseg?file=src%2FApp.tsx
It's certainly not ideal but I think this might be a TypeScript limitation regarding inference, generics, and function props. Hope that helps! ✨
What is the difference between the listbox and combobox in this? Since this works perfectly fine with the listbox.
type GenericListboxProps<TValue,> = {
value: Readonly<TValue>;
};
interface SingleListboxProps extends GenericListboxProps<string | null> {
multiple: false;
onChange: (value: string) => void;
}
interface MultiListboxProps extends GenericListboxProps<string[]> {
multiple: true;
onChange: (value: string[]) => void;
}
export default function Select(props: SingleListboxProps | MultiListboxProps) {
return (
<Listbox
value={props.value}
onChange={props.onChange}
>
</Listbox>
)
}
but with the combobox this throws an error
The expected type comes from property 'multiple' which is declared here on type 'IntrinsicAttributes & { value?: string | readonly string[] | null | undefined; nullable?: false | undefined; multiple?: false | undefined; defaultValue?: string | readonly string[] | null | undefined
For me this happened with <Combobox /> when the onChange prop was receiving the setter of useState directly, It got fixed when I changed this:
<Combobox
multiple
value={selectedPeople}
onChange={setSelectedPeople}
/>
to:
<Combobox
multiple
value={selectedPeople}
onChange={(value) => setSelectedPeople(value)}
/>