question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue
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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
silver-snoopycommented, Feb 27, 2021

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

1reaction
xiaoyu-tamucommented, Jan 27, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Autocomplete] options does not accept const assertions array ...
The type 'readonly ["a", "b"]' is 'readonly' and cannot be assigned to the mutable type 'unknown[]'.ts(4104) useAutocomplete.d.ts(231, ...
Read more >
material ui autocomplete doesn't recognize options parameter ...
My assumption is that when you do: setOptions(res.data); you are setting the options to an object, not an array. In fact the error...
Read more >
TypeScript: JavaScript With Syntax For Types.
TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale. Property 'name' does not exist...
Read more >
Deep dive into enums, const assertions and union types
Typescript is a useful value addition to the JavaScript world. ... In this blog, I will talk about enums, const assertions and union...
Read more >
Untitled
What is Zod Zod is a TypeScript-first schema declaration and validation library. I'm using the term "schema" to broadly refer to any data...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found