mui / material-ui

Material UI: Comprehensive React component library that implements Google's Material Design. Free forever.
https://mui.com/material-ui/
MIT License
93.76k stars 32.25k forks source link

[joy-ui][Autocomplete] Typescript & React runtime error using Autocomplete #39219

Open Onurfesci opened 1 year ago

Onurfesci commented 1 year ago

Duplicates

Latest version

Steps to reproduce 🕹

Trying to use Joy UI's Autocomplete in a controlled state as per the docs, I get a TypeScript error: image

I also get a runtime error on the browser from React. image

I've tried passing objects with value and label as options, passing a value prop, but I also get the same typescript error. It expects it to be an array of string or the inferred type from option. I also get errors if I pass it an array.

Am I using this component wrong, or is there an issue with these types?

Reproduction: (MUI Joy's own example has type errors) https://codesandbox.io/s/cp2zmj?file=/Demo.tsx

Current behavior 😯

No response

Expected behavior 🤔

No response

Context 🔦

No response

Your environment 🌎

npx @mui/envinfo ``` Don't forget to mention which browser you used. Output from `npx @mui/envinfo` goes here. ```
mj12albert commented 1 year ago

@Onurfesci Thanks for reporting this ~ I can reproduce it in our docs example

auvansang commented 1 year ago

I have the same issue as above

siriwatknp commented 1 year ago

I will take a look tomorrow.

siriwatknp commented 1 year ago

Based on the CodeSandbox you provided, I guess there is something wrong with CodeSandbox because I copied the code and put it in my local react-vite (from scratch) + Joy UI without any type errors.

Could you provide the code that you use in your local project?

auvansang commented 1 year ago

@siriwatknp you can check on this codesandbox, press f12, click on the dropdown icon and the error will occur

https://codesandbox.io/p/sandbox/spring-browser-mypv5f

siriwatknp commented 1 year ago

@siriwatknp you can check on this codesandbox, press f12, click on the dropdown icon and the error will occur

https://codesandbox.io/p/sandbox/spring-browser-mypv5f

I tried but what's the error that I should see? Is it related to this issue?

auvansang commented 1 year ago

You can see the issue like bellow, for the issue on the issue "Property id ...", I cannot duplicate it on my codesandbox

image

mj12albert commented 1 year ago

Looks like a similar issue to https://github.com/mui/material-ui/issues/39474

Though I can't reproduce the issue with Joy anymore (even with Next.js like in @sang-au's sandbox) https://codesandbox.io/p/sandbox/sleepy-liskov-fvjhmf?file=%2Fsrc%2Fapp%2Fpage.tsx%3A1%2C1-21%2C1

Onurfesci commented 1 year ago

I copied my implementation as-is to codesandbox, but I'm not seeing the same issues that I'm seeing on my VSCode. Really strange...

Sandbox repro (No TS Errors): https://codesandbox.io/s/billowing-smoke-vpcn32?file=/src/Demo.tsx

Local VSCode (Getting type errors on 3 props: value, getOptionLabel, onChange). The inference seems to be completely different. For some reason, on my VSCode, it is inferring value as an array of string or the inferred option type, rather than the option type itself. If I @ts-ignore, it works perfectly so I'm not implementing it wrong. See the images below.

Type error on value: image

Type error on getOptionLabel (Thinks option is the option or a string?): image

Type error on onChange: image

Notes:

Does anybody have an idea why this is happening? Could it be a tsconfig issue with Next.js projects, clashing with Joy's type inferences?

Onurfesci commented 1 year ago

I got the codesandbox above to show some TS errors now. Basically, if the value a string and the options are an array of objects, the Autocomplete isn't happy. So what I want to ask to the Joy team is...

siriwatknp commented 1 year ago

Perhaps the generic type of AutocompleteProps should allow for another parameter, TValue? This way the default type casting could be overridden if needed? Better yet, I think the default casting of value type should be from the value prop itself

I think so, this would let developers gain more control over the types they want. Since this is related to Base UI useAutocomplete, I am tagging @michaldudak and @mj12albert for further discussion.

michaldudak commented 1 year ago

This is already being tracked in #23708.