Incorrect type for `ref` prop in components created by `createLeafComponent` helper in React Aria Component
Provide a general summary of the issue here
Hello ๐
All components that are created with createLeafComponent helper (e.g. MenuItem, ListBoxItem) have incorrect type for the ref prop, because it's inferred from incorrect generic in the code (value type, instead of element)
If I try to console.log the ref value though it's correctly an underlying div element:
๐ค Expected Behavior?
ref prop should be properly typed as some kind of a HTML element
๐ฏ Current Behavior
ref prop is typed with the generic for value
๐ Possible Solution
It seems like the generics here are a bit off:
https://github.com/adobe/react-spectrum/blob/826d9d450ab075f6e7aa6b4cdcda944b19b12acb/packages/%40react-aria/collections/src/CollectionBuilder.tsx#L159-L161
Currently both of React.RefAttributes in the return types utilize T generic which is used for the generic value of the item. If I change both of them to utilize E which is for element type, the component is properly typed now, but I've got an error by your ESLint configuration that generic T is defined but never used. Hence I didn't provide the PR ๐ But I hope my investigation will be helpful here.
Before:
After:
๐ฆ Context
At my work we've got internal design system built upon RAC and after upgrading to the latest version I've spotted that we're getting type errors when passing refs to components
๐ฅ๏ธ Steps to Reproduce
https://stackblitz.com/edit/vitejs-vite-7jspea?file=src%2FApp.tsx
- Try to render e.g.
MenuwithMenuItem - Pass value to
MenuItem - Try to pass a ref to
MenuItem - Observe type error. It'll be either on the
refthat it's not assignable to the value type or on thevaluethat it's not assignable toHTMLDivElement.
I've used stackblitz instead of codesanbox, because Codesandbox with CRA didn't work for me properly (some strange errors and worse TS support).
Version
1.3.1 React Aria Components
What browsers are you seeing the problem on?
Other
If other, please specify.
N/A
What operating system are you using?
N/A
๐งข Your Company/Team
No response
๐ท Tracking Issue
No response
Thanks for the issue and initial investigation! Looks like a bug.
I was also just hit by this and discovered this after looking into the .d.ts files that are shipping with React Aria Components.
- export const MenuItem: <T extends object>(props: MenuItemProps<T> & React.RefAttributes<T>) => React.ReactElement | null;
+ export const MenuItem: <T extends object, E extends Element>(props: MenuItemProps<T> & React.RefAttributes<E>) => React.ReactElement | null;
I don't know if these types are generated by this createLeafComponent helper mentioned by @pawelblaszczyk5 or if these typings are separate. Would be great to get this fixed!
Is there already a PR open to address this issue? I took a quick scan, but didn't find anything obvious.
nothing at the moment if you want to make a contribution
closed by https://github.com/adobe/react-spectrum/pull/8507