[Select] `renderValue` has incorrect TypeScript types when `displayEmpty` is `true`
@ZeeshanTamboli is already working on this.
Since Sep 2, 2022.
- Dominant language
- JavaScript
- Stars
- 99.1k
- Forks
- 32.5k
- Avg merge
- 2d 17h
- Merged PRs (30d)
- 106
Description
Duplicates
- I have searched the existing issues
Latest version
- I have tested the latest version
Current behavior 😯
For the Select component, as of https://github.com/mui/material-ui/pull/33088, value accepts T | '' as the type, but renderValue still only has the type (value: T) => React.ReactNode. This is incorrect since if displayEmpty is true, renderValue can actually be passed an empty string '' as well. This leads to TS errors when trying to use renderValue and handling the empty-string case.
Expected behavior 🤔
renderValue should be typed to indicate that it can be passed an empty string, if displayEmpty istrue.
Steps to reproduce 🕹
See sandbox here https://codesandbox.io/s/jolly-julien-qr1xlr?file=/src/App.tsx and notice the TS error in renderValue:
This condition will always return 'false' since the types 'MyEnum' and '""' have no overlap.ts(2367)
Context 🔦
It seems this problem was partly uncovered/introduced in https://github.com/mui/material-ui/pull/33088. Prior to that change, you could set the type of the value as something like const value: MyEnum | "" = ""; and the generic T for the Select component would be MyEnum | "". Now when you do that, however, T ends up as MyEnum and renderValue thus only handles the case that a value is selected, even when displayEmpty is true. One seemingly hacky workaround (that disagrees slightly with the intention of that PR) is to do <Select<MyEnum | ""> ... />, but it seems the real solution is to have renderValue provide T | "" when displayEmpty is true.
Your environment 🌎
npx @mui/envinfo
❯ npx @mui/envinfo
npx: installed 2 in 1.324s
System:
OS: macOS 12.4
Binaries:
Node: 14.18.2 - ~/.nvm/versions/node/v14.18.2/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 6.14.15 - ~/.nvm/versions/node/v14.18.2/bin/npm
Browsers:
Chrome: 104.0.5112.101
Edge: Not Found
Firefox: 103.0
Safari: 15.5
npmPackages:
@emotion/react: ^11.7.1 => 11.7.1
@emotion/styled: ^11.6.0 => 11.6.0
@mui/base: 5.0.0-alpha.87
@mui/core-downloads-tracker: 5.10.2
@mui/icons-material: ^5.8.4 => 5.10.2
@mui/lab: ^5.0.0-alpha.88 => 5.0.0-alpha.88
@mui/material: ^5.8.6 => 5.10.2
@mui/private-theming: 5.9.3
@mui/styled-engine: 5.10.2
@mui/styles: ^5.8.6 => 5.8.6
@mui/system: ^5.8.6 => 5.10.2
@mui/types: 7.1.5
@mui/utils: 5.9.3
@mui/x-data-grid: ^5.2.1 => 5.2.1
@mui/x-date-pickers: ^5.0.0-alpha.5 => 5.0.0-beta.7
@types/react: ^17.0.19 => 17.0.19
react: ^17.0.2 => 17.0.2
react-dom: ^17.0.2 => 17.0.2
typescript: ^4.4.3 => 4.4.3
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.