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

Autocomplete expect string | string[] #35308

Closed
2 tasks done
hortizgarrido opened this issue Dec 1, 2022 · 8 comments · Fixed by #35367
Closed
2 tasks done

Autocomplete expect string | string[] #35308

hortizgarrido opened this issue Dec 1, 2022 · 8 comments · Fixed by #35367
Labels
bug 🐛 Something doesn't work component: autocomplete This is the name of the generic UI component, not the React module!

Comments

@hortizgarrido
Copy link

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Steps to reproduce 🕹

No response

Current behavior 😯

Should not have to explicitly set the type in autocomplete

Expected behavior 🤔

Upgrading to latest MUI/material version is giving errors on how the autocomplete infers the type. I use to just be able to have <Autocomplete
.... />

but now i have to put <Autocomplete ...../>

Context 🔦

Issue affects entire code. Would need major rework.

Your environment 🌎

npx @mui/envinfo
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.
@hortizgarrido hortizgarrido added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Dec 1, 2022
@huyle2405
Copy link

Same issue with the latest version.

Revert to version 5.10.15 will be ok.

@zannager zannager added the component: autocomplete This is the name of the generic UI component, not the React module! label Dec 1, 2022
@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Dec 1, 2022

@hortizgarrido @huyle2405 Can you provide a CodeSandbox reproducing the issue or a minimal code example that reproduces the problem. The issue template is a good starting point.

@ZeeshanTamboli ZeeshanTamboli added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Dec 1, 2022
@DaveITpl
Copy link

DaveITpl commented Dec 2, 2022

useAutocomplete.d.ts
export type AutocompleteValue<T, Multiple, DisableClearable, FreeSolo> = Multiple extends true

default property is false

@gnowland
Copy link
Contributor

gnowland commented Dec 5, 2022

This bug was introduced by #35275. Since that PR was merged omitting Autocomplete parameter multiple results in TypeScript inferring that the multiple parameter is true when it should be false by default (according to the API docs).

Until this bug is fixed you can fix the error by explicitly setting multiple={false} on all Autocomplete components.

@fenghan34
Copy link
Contributor

fenghan34 commented Dec 5, 2022

The default type of multiple prop is undefined if you don’t specify it, which can be found by hovering on <Autocomplete ... /> or checking type declarations in source code.

The PR #35275 I made was to fix the incorrect inferring type of AutocompleteValue when multiple is true, which worked as expected and passed all typescript tests. And the inferred type should be NonNullable<T | AutocompleteFreeSoloValueMapping<FreeSolo>> or T | null | AutocompleteFreeSoloValueMapping<FreeSolo> if Multiple is undefined(default) or false as follows.

export type AutocompleteValue<T, Multiple, DisableClearable, FreeSolo> = Multiple extends true
  ? Array<T | AutocompleteFreeSoloValueMapping<FreeSolo>>
  : DisableClearable extends true
  ? NonNullable<T | AutocompleteFreeSoloValueMapping<FreeSolo>>
  : T | null | AutocompleteFreeSoloValueMapping<FreeSolo>;

Please educate me if I misunderstand somewhere. And I still can’t reproduce this problem locally, so It would be very helpful to debug it if someone could provide the code example and environment reproducing the problem.

@gnowland
Copy link
Contributor

gnowland commented Dec 5, 2022

@fenghan34 I've replicated the issue affecting #35275 in a CodeSandbox: https://codesandbox.io/s/bold-thunder-h6zyo2.

The props I included in the example are value and onChange because these are the props that were affected by this bug in my codebase, but there may be other props of the Autocomplete component that would also cause this error that I'm unaware of.

@fenghan34
Copy link
Contributor

fenghan34 commented Dec 6, 2022

@gnowland Thanks!

It turns out the AutocompleteValue type would be inferred to T[] if you disable strictNullChecks in your project since TypeScript would ignore undefined and allow it to extend any type except never. Here's the demonstration.

And It hasn't been detected in TypeScript tests as strictNullChecks is enabled in MUI codebase. I'm not sure if need to improve the TypeScript test workflow, and I've made a PR to fix this issue.

@gnowland
Copy link
Contributor

gnowland commented Dec 6, 2022

Thanks for explaining!! I’m still a bit befuddled by complex TypeScript declarations, glad I could help define the issue for you to fix :)

@ZeeshanTamboli ZeeshanTamboli added bug 🐛 Something doesn't work and removed status: waiting for author Issue with insufficient information labels Dec 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: autocomplete This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants