Skip to content
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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
29 changes: 27 additions & 2 deletions packages/mui-material/src/useAutocomplete/useAutocomplete.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,34 @@ export type AutocompleteGetTagProps = ({ index }: { index: number }) => {
*
* - [useAutocomplete API](https://mui.com/base-ui/react-autocomplete/hooks-api/#use-autocomplete)
*/
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> & {
groupBy: (option: Value) => string;
},
): UseAutocompleteReturnValue<Value, Multiple, DisableClearable, FreeSolo, true>
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> & {
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>;
): UseAutocompleteReturnValue<Value, Multiple, DisableClearable, FreeSolo, unknown>;

export interface UseAutocompleteRenderedOption<Value> {
option: Value;
Expand All @@ -372,6 +392,7 @@ export interface UseAutocompleteReturnValue<
Multiple extends boolean | undefined = false,
DisableClearable extends boolean | undefined = false,
FreeSolo extends boolean | undefined = false,
HasGroupBy extends boolean | unknown = false,
> {
/**
* Resolver for the root slot's props.
Expand Down Expand Up @@ -462,7 +483,11 @@ export interface UseAutocompleteReturnValue<
/**
* The options to render. It's either `Value[]` or `AutocompleteGroupedOption<Value>[]` if the groupBy prop is provided.
*/
groupedOptions: Value[] | Array<AutocompleteGroupedOption<Value>>;
groupedOptions: HasGroupBy extends true
? AutocompleteGroupedOption<Value>[]
: HasGroupBy extends false
? Value[]
: Value[] | AutocompleteGroupedOption<Value>[];
}

export default useAutocomplete;
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { expectType } from '@mui/types';
import { useAutocomplete, FilterOptionsState } from '@mui/material/useAutocomplete';
import {
useAutocomplete,
FilterOptionsState,
AutocompleteGroupedOption,
} from '@mui/material/useAutocomplete';

interface Person {
id: string;
Expand Down Expand Up @@ -181,4 +185,17 @@ function Component() {
},
freeSolo: true,
});

const ungroupedAutocomplete = useAutocomplete({ options: persons });
expectType<Person[], typeof ungroupedAutocomplete.groupedOptions>(
ungroupedAutocomplete.groupedOptions,
);

const groupedAutocomplete = useAutocomplete({
options: persons,
groupBy: ({ id }) => id,
});
expectType<AutocompleteGroupedOption<Person>[], typeof groupedAutocomplete.groupedOptions>(
groupedAutocomplete.groupedOptions,
);
}
Loading