-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Improve Combobox
types to improve false positives
#2411
Conversation
If you use the default `Combobox` component then it defaults to `Fragment`. This also means that if you provide an additional prop that it would be forwarded to the `Fragment` but that won't work. You do get a runtime error, but the types aren't 100% clear on what's going on. In fact, they make it very confusing because it will use the last "fallback" in all the `Combobox` definitions which marks the value as "multiple". Concretely, this means: ```ts let [value, setValue] = useState('Tom Cook') <Combobox value={value} onChange={setValue} placeholder="Hello!" /> ``` Starts complaining about the `value` and `onChange` not handling the `multiple` case correctly. But it should complain about the `placeholder`. Switching the order _does_ solve this, but it is not the cleanest solution. Maybe we should be explicit about the `Fragment` case somehow. However, there is a use case where I don't think TypeScript will be able to help and it's a bit unfortunate. ```ts let [value, setValue] = useState('Tom Cook') <Combobox value={value} onChange={setValue} placeholder="Hello!"> <div> {/* ... */} </div> </Combobox> ``` This is valid because at runtime we will forward all the props to the `div`. So not 100% sure what we should do here instead.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
): JSX.Element | ||
<TValue, TTag extends ElementType = typeof DEFAULT_COMBOBOX_TAG>( | ||
props: ComboboxProps<TValue, false, true, TTag> & RefProp<typeof ComboboxFn> | ||
props: ComboboxProps<TValue, false, false, TTag> & RefProp<typeof ComboboxFn> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fact that this change fixes things amazes me. Thanks TypeScript 😂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
If you use the default
Combobox
component then it defaults toFragment
. This also means that if you provide an additional prop that it would be forwarded to theFragment
but that won't work.You do get a runtime error, but the types aren't 100% clear on what's going on. In fact, they make it very confusing because it will use the last "fallback" in all the
Combobox
definitions which marks the value as "multiple".Concretely, this means:
Starts complaining about the
value
andonChange
not handling themultiple
case correctly. But it should complain about theplaceholder
.Switching the order does solve this, but it is not the cleanest solution.
Maybe we should be explicit about the
Fragment
case somehow.However, there is a use case where I don't think TypeScript will be able to help and it's a bit unfortunate.
This is valid because at runtime we will forward all the props to the
div
. So not 100% sure what we should do here instead.Visually, these changes look like:
![image](https://user-images.githubusercontent.com/1834413/229560139-b555961a-4c95-45bd-b6a7-1e216b729473.png)
vs
![image](https://user-images.githubusercontent.com/1834413/229560162-50f48a5d-7e8e-40f6-abc4-36f5230f0d5c.png)
Fixes: #2407