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 typeof groupOptions in useAutocomplete #40739

Open
3 tasks
lewxdev opened this issue Jan 22, 2024 · 2 comments
Open
3 tasks

[useAutocomplete] Improve typeof groupOptions in useAutocomplete #40739

lewxdev opened this issue Jan 22, 2024 · 2 comments
Assignees
Labels
component: autocomplete This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature package: base-ui Specific to @mui/base typescript

Comments

@lewxdev
Copy link

lewxdev commented Jan 22, 2024

What's the problem?

Currently, the following type is true regardless of the inputs passed in to useAutocomplete:

// pseudocode
typeof UseAutocompleteReturnValue<Value, ...>["groupedOptions"] = Value[] | AutocompleteGroupedOption<Value>[]

Essentially, the developer has to narrow or cast the type despite MUI being able to assert the correct type definition with the inputs of the useAutocomplete hook. I propose addressing the type definition here

What are the requirements?

  • Validate the new types are valid in the given scenarios
    • When groupBy is passed typeof groupedOptions = AutocompleteGroupedOption<T>[]
    • When groupBy is omitted, typeof groupedOptions = T[]
  • Validate the implementation doesn't interfere with existing types

What are our options?

  • Leave as is

Proposed solution

We could do something like

useAutocomplete.d.ts

- function useAutocomplete<
-   Value,
-   Multiple extends boolean | undefined = false,
-   DisableClearable extends boolean | undefined = false,
-   FreeSolo extends boolean | undefined = false,
+   GroupBy extends ((option: Value) => string) | undefined = undefined,
- >(
-   props: UseAutocompleteProps<Value, Multiple, DisableClearable, FreeSolo>
+   props: UseAutocompleteProps<Value, Multiple, DisableClearable, FreeSolo, GroupBy>
- ): UseAutocompleteReturnValue<Value, Multiple, DisableClearable, FreeSolo>
+ ): UseAutocompleteReturnValue<Value, Multiple, DisableClearable, FreeSolo, GroupBy>

Effective implementation (abbreviated)

interface UseAutocompleteProps<
  Value,
  // ...,
  GroupBy extends ((option: Value) => string) | undefined = undefined
> {
  // ...,
  groupBy?: GroupBy;
}

interface UseAutocompleteReturnValue<
  Value,
  // ...,
  GroupBy extends ((option: Value) => string) | undefined = undefined
> {
  // ...,
  groupedOptions: GroupBy extends undefined ? Value[] : AutocompleteGroupedOption<Value>[];
}

Resources and benchmarks

This was pointed out by @spencermehta

Shouldn't the type here actually be conditional upon the type of the groupBy prop? If it is not provided the type should be T[] and if it is provided it should be Array<AutocompleteGroupedOption<T>>. With the current implementation I'm forced to handle the case that the options are of type T[] even if I've provided the groupBy prop.

Minimal working playground link

Search keywords:

@lewxdev lewxdev added RFC Request For Comments status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jan 22, 2024
@lewxdev lewxdev changed the title [RFC] Improve groupOptions Type in useAutocomplete [RFC] Improve typeof groupOptions in useAutocomplete Jan 22, 2024
@zannager zannager added the component: autocomplete This is the name of the generic UI component, not the React module! label Jan 22, 2024
@ZeeshanTamboli
Copy link
Member

Essentially, the developer has to narrow or cast the type despite MUI being able to assert the correct type definition with the inputs of the useAutocomplete hook.

Can you show an example where you needed to narrow or cast the type? Your request makes sense to me. I don't think it would cause any issues or a breaking change. Want to give it a try by making a pull request?

@ZeeshanTamboli ZeeshanTamboli added typescript package: base-ui Specific to @mui/base enhancement This is not a bug, nor a new feature and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer RFC Request For Comments labels Feb 6, 2024
@ZeeshanTamboli ZeeshanTamboli changed the title [RFC] Improve typeof groupOptions in useAutocomplete [useAutocomplete] Improve typeof groupOptions in useAutocomplete Feb 6, 2024
@lewxdev
Copy link
Author

lewxdev commented Feb 26, 2024

Want to give it a try by making a pull request?

@ZeeshanTamboli I'll give it a shot. Starting to look into this now 👍🏽

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: autocomplete This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature package: base-ui Specific to @mui/base typescript
Projects
None yet
Development

No branches or pull requests

4 participants