-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[useAutocomplete] groupedOptions type definition seems wrong #23846
Comments
@ZachCMP Thanks for the detailed report. The types look definitely wrong. Would this be enough? diff --git a/packages/material-ui/src/useAutocomplete/useAutocomplete.d.ts b/packages/material-ui/src/useAutocomplete/useAutocomplete.d.ts
index cec10d8e08..8017cc3996 100644
--- a/packages/material-ui/src/useAutocomplete/useAutocomplete.d.ts
+++ b/packages/material-ui/src/useAutocomplete/useAutocomplete.d.ts
@@ -271,6 +271,14 @@ export type AutocompleteChangeReason =
export interface AutocompleteChangeDetails<T = string> {
option: T;
}
+
+export interface AutocompleteGroupedOption<T = string> {
+ key: number;
+ index: number;
+ group: string;
+ options: T[];
+}
+
export type AutocompleteCloseReason =
| 'toggleInput'
| 'escape'
@@ -311,5 +319,5 @@ export default function useAutocomplete<
anchorEl: null | HTMLElement;
setAnchorEl: () => void;
focusedTag: number;
- groupedOptions: T[];
+ groupedOptions: AutocompleteGroupedOption<T>[];
}; |
@oliviertassinari Thanks for the quick response! Looking at the That would remove the type errors, but I think it would still require you to type guard when you implement it. It seems like the switch is the presence of a interface AutocompleteWithNormalOptions<T> {
groupedOptions: T[]
groupBy: (option: T) => string
}
interface AutocompleteWithGroupedOptions<T> {
groupedOptions: AutocompleteGroupedOption<T>[]
groupBy?: undefined
} I'm not sure exactly how you'd go about weaving that into the rest of the props here. I've mostly used type Props<...args> = UseAutocompleteProps<...args> & (AutocompleteWithNormalOptions<T> | AutocompleteWithGroupedOptions<T>) But I know that there's a few different ways to do that kind of thing, and I'm not sure which one is most appropriate here. What do you think? |
Sorry for the double post, but it occurs to me that another option would be to output groupedOptions: AutocompleteGroupedOption<T>[]
filteredOptions: T[] That might be more in line with what the variable names indicate, and prevents having to do a type check. However, it would also require some slight tweaking of the |
@ZachCMP A right, I had forgotten about this. I would be leaning for the "quick win" for a start (the union type), at least we would export |
@oliviertassinari Yeah, I think a union type of |
@ZachCMP If you could open a pull request, that would definitely help :) |
Hey, I think I found a mistake in the type definitions for
useAutocomplete
. Happy to take a crack at fixing it, but I wanted to put an issue up first to make sure it wasn't like that for a reason.Current Behavior 😯
I'm getting these type errors when using
groupedOptions
from theuseAutocomplete
hook, when using the actual code in theAutocomplete
component that consumesgroupedOptions
.The type mapping indicates that
groupedOptions
isT[]
, but the code inuseAutocomplete
seems to actually return a different type ifgroupBy
is defined. Here's the relevant code fromuseAutocomplete
:And the relevant type definition in
useAutocomplete.d.ts
:Expected Behavior 🤔
I shouldn't get type errors here. Ideally the typing should be
T[]
ifgroupBy
isn't defined, and something like{ key: number, index: number, group: string, options: T[] }
if it is defined.Steps to Reproduce 🕹
TS playground was having trouble importing
@material-ui/lab
, so I just used codesandbox, which was closer to the errors I'm actually getting in my editor.https://codesandbox.io/s/material-ui-issue-forked-ky88t?file=/src/Demo.tsx
Steps:
Context 🔦
I'm working on a custom autocomplete component that allows more granular control around the behavior of the
freeSolo
aspect. The stock Autocomplete behavior for that doesn't work for my purposes here, so I'm building something custom to allow the addition of actions as additional options, which would be in their own group. These TS errors appeared when copying some pieces of the stockAutocomplete
component into my codebase. Since it's a custom component, I can cast the types manually, so it's not holding me up now; But the type definition seems obviously wrong, no?Your Environment 🌎
The text was updated successfully, but these errors were encountered: