-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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] Improve TS typing of groupedOptions
prop
#44657
base: master
Are you sure you want to change the base?
Conversation
Netlify deploy previewhttps://deploy-preview-44657--material-ui.netlify.app/ Bundle size report |
UseAutocompleteReturnValue["groupedOptions"]
UseAutocompleteReturnValue["groupedOptions"]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lewxdev What issue are facing by directly passing it through UseAutocompleteProps
which you proposed in the issue?
@ZeeshanTamboli because groupBy is a function (unlike the other generic props), I had trouble getting the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lewxdev What about the following changes:
--- a/packages/mui-material/src/useAutocomplete/useAutocomplete.d.ts
+++ b/packages/mui-material/src/useAutocomplete/useAutocomplete.d.ts
@@ -373,14 +373,6 @@ export function useAutocomplete<
groupBy?: undefined;
},
): UseAutocompleteReturnValue<Value, Multiple, DisableClearable, FreeSolo, false>;
-export function useAutocomplete<
- Value,
- Multiple extends boolean | undefined = false,
- DisableClearable extends boolean | undefined = false,
- FreeSolo extends boolean | undefined = false,
->(
- props: UseAutocompleteProps<Value, Multiple, DisableClearable, FreeSolo>,
-): UseAutocompleteReturnValue<Value, Multiple, DisableClearable, FreeSolo, unknown>;
export interface UseAutocompleteRenderedOption<Value> {
option: Value;
@@ -392,7 +384,7 @@ export interface UseAutocompleteReturnValue<
Multiple extends boolean | undefined = false,
DisableClearable extends boolean | undefined = false,
FreeSolo extends boolean | undefined = false,
- HasGroupBy extends boolean | unknown = false,
+ HasGroupBy extends boolean = false,
> {
/**
* Resolver for the root slot's props.
@@ -483,11 +475,7 @@ export interface UseAutocompleteReturnValue<
/**
* The options to render. It's either `Value[]` or `AutocompleteGroupedOption<Value>[]` if the groupBy prop is pr
ovided.
*/
- groupedOptions: HasGroupBy extends true
- ? AutocompleteGroupedOption<Value>[]
- : HasGroupBy extends false
- ? Value[]
- : Value[] | AutocompleteGroupedOption<Value>[];
+ groupedOptions: HasGroupBy extends true ? AutocompleteGroupedOption<Value>[] : Value[];
}
export default useAutocomplete;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will also need to change the description of groupedOptions
here. My suggestion:
/**
* The options to render.
* - If `groupBy` is provided, the options are grouped and represented as `AutocompleteGroupedOption<Value>[]`.
* - Otherwise, the options are represented as a flat array of `Value[]`.
*/
UseAutocompleteReturnValue["groupedOptions"]
groupedOptions
type
groupedOptions
typegroupedOptions
prop
@ZeeshanTamboli the issue with the first proposed change is if someone passes an option to options:
|
@lewxdev I didn’t fully understand this. Does the issue come from the proposed change I mentioned here? Could you provide a TS playground example to explain? |
@ZeeshanTamboli ah, that was a misunderstanding on my part. I've not worked with declaration files in this way before and thought the overload required an implementation signature. after looking into the documentation on overloaded function declarations, I understand your proposed change. all proposed changes have been made |
…ewxdev/material-ui into improve-use-autocomplete-types
…ewxdev/material-ui into improve-use-autocomplete-types
@lewxdev I noticed the This might be caused by intersecting types in the overload. Can we correct it? |
resolves #40739