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] options does not accept const assertions array with TypeScript #24639

Closed
2 tasks done
michael-land opened this issue Jan 27, 2021 · 8 comments · Fixed by #25155
Closed
2 tasks done

[Autocomplete] options does not accept const assertions array with TypeScript #24639

michael-land opened this issue Jan 27, 2021 · 8 comments · Fixed by #25155
Labels
component: autocomplete This is the name of the generic UI component, not the React module! ready to take Help wanted. Guidance available. There is a high chance the change will be accepted typescript

Comments

@michael-land
Copy link
Contributor

michael-land commented Jan 27, 2021

export const a = ['a', 'b'] as const;
const D = () => <Autocomplete options={a} />;

ts Error

The type 'readonly ["a", "b"]' is 'readonly' and cannot be assigned to the mutable type 'unknown[]'.ts(4104)


useAutocomplete.d.ts(231, 3): The expected type comes from property 'options' which is declared here on type 'IntrinsicAttributes & AutocompleteProps<unknown, undefined, undefined, undefined, "div">'

change the type to options: Readonly<T[]>; could fix the issue.

diff --git a/node_modules/@material-ui/core/useAutocomplete/useAutocomplete.d.ts b/node_modules/@material-ui/core/useAutocomplete/useAutocomplete.d.ts
index 4e01bb5..5ccd71c 100644
--- a/node_modules/@material-ui/core/useAutocomplete/useAutocomplete.d.ts
+++ b/node_modules/@material-ui/core/useAutocomplete/useAutocomplete.d.ts
@@ -228,7 +228,7 @@ export interface UseAutocompleteProps<
   /**
    * Array of options.
    */
-  options: T[];
+  options: Readonly<T>[];
   /**
    * If `true`, the input's text is selected on focus.
    * It helps the user clear the selected value.
  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

TS Error

Expected Behavior 🤔

No TS Error

Your Environment 🌎

`npx @material-ui/envinfo`
  Don't forget to mention which browser you used.
  Output from `npx @material-ui/envinfo` goes here.

npx: installed 2 in 1.753s

System:
OS: macOS 10.15.6
Binaries:
Node: 14.7.0 - /var/folders/7w/t3q6v18575b1rncctdm639040000gn/T/fnm-shell-709267/bin/node
Yarn: 1.22.10 - /usr/local/bin/yarn
npm: 6.14.7 - /var/folders/7w/t3q6v18575b1rncctdm639040000gn/T/fnm-shell-709267/bin/npm
Browsers:
Chrome: 88.0.4324.96
Edge: Not Found
Firefox: 84.0.1
Safari: 14.0
npmPackages:
@emotion/react: 11.1.4 => 11.1.4
@emotion/styled: 11.0.0 => 11.0.0
@material-ui/core: next => 5.0.0-alpha.22
@material-ui/icons: next => 5.0.0-alpha.22
@material-ui/lab: next => 5.0.0-alpha.22
@material-ui/styled-engine: 5.0.0-alpha.22
@material-ui/styles: 5.0.0-alpha.22
@material-ui/system: 5.0.0-alpha.22
@material-ui/types: 5.1.4
@material-ui/unstyled: 5.0.0-alpha.22
@material-ui/utils: 5.0.0-alpha.22
@types/react: 17.0.0 => 17.0.0
react: 17.0.1 => 17.0.1
react-dom: 17.0.1 => 17.0.1
typescript: 4.1.3 => 4.1.3

@michael-land michael-land added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jan 27, 2021
@michael-land
Copy link
Contributor Author

michael-land commented Jan 27, 2021

I'm not sure this is a bug or intend behavior, maybe this should be a feature request?

@oliviertassinari oliviertassinari added component: autocomplete This is the name of the generic UI component, not the React module! typescript labels Jan 27, 2021
@oliviertassinari
Copy link
Member

@xiaoyu-tamu I have tried your fix but I can still see the error. I have no insights on this case.

@michael-land
Copy link
Contributor Author

michael-land commented Jan 27, 2021

@oliviertassinari My bad, I made a typo. could you try Readonly<T[]> instead,

@oliviertassinari
Copy link
Member

@xiaoyu-tamu Ok, the TypeScript tests pass with the proposed change. Do you want to send a pull request (with a quick test under (packages/material-ui/src/useAutocomplete/useAutocomplete.spec.ts)?

@oliviertassinari oliviertassinari changed the title [Typescript] Autocomplete options does not accept const assertions array [Autocomplete] options does not accept const assertions array with TypeScript Jan 27, 2021
@oliviertassinari oliviertassinari added ready to take Help wanted. Guidance available. There is a high chance the change will be accepted and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jan 27, 2021
@michael-land
Copy link
Contributor Author

Sure, i'll give it a try, never write type test before.

@silver-snoopy
Copy link
Contributor

Hey @xiaoyu-tamu, @oliviertassinari - Would you mind If I open a PR for this? Happy to take it

@eps1lon
Copy link
Member

eps1lon commented Mar 1, 2021

@silver-snoopy Sure thing, go ahead.

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! ready to take Help wanted. Guidance available. There is a high chance the change will be accepted typescript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants