petyosi / react-virtuoso

The most powerful virtual list component for React
https://virtuoso.dev
MIT License
5.25k stars 301 forks source link

Infer type of 2nd positional parameter in itemContent callback #233

Closed bb closed 3 years ago

bb commented 3 years ago

Hi Petyo,

thanks for creating react-virtuoso. It works really great for me.

I just upgraded to 1.0 and wanted to try the new data parameter in itemContent and noticed it was typed any instead of the inferable type from <Virtuoso data={...} ... prop.

I think it can be added like this:

export interface VirtuosoProps<ItemType extends any> extends Omit<ListProps, 'groupCounts' | 'groupContent'> {
...
  data?: ItemType[]
...
  itemContent?: (index: number, data?: ItemType) => ReactNode
...

export interface GroupedVirtuosoProps<ItemType extends any>
  extends Omit<VirtuosoProps<ItemType>, 'totalCount' | 'itemContent'>,
...

However, I don't know to parameterize the export const lines at the very end of components.tsx, yet. Maybe you do? If you have a pointer how to fix that, I might be able to provide a PR.

petyosi commented 3 years ago

@bb - Me neither, TBH. I googled several "generic React components" phrases, but nothing seems to make sense.

bb commented 3 years ago

Thanks for the quick reply! I don't have the time to investigate deeper right now, but I'll give it another try later. Or maybe somebody else chimes in. 😉

bb commented 3 years ago

For Future-Me or whoever implements that: I just noticed that this would ideally also apply to data property of Item and RecordItem as well as ItemContent and GroupItemContent (defined in interfaces.ts). This would help e.g. in itemsRendered.

petyosi commented 3 years ago

I don't think (but I might be wrong) that you have to interfere with interfaces.ts. The "official" typings of the component are re-declared in components.ts for documentation purposes. While they do extend some interfaces from interfaces.ts, this is mostly to avoid duplication.

seahorsepip commented 3 years ago

I had a try at adding generic typing to the Virtuoso and GroupedVirtuoso components in the PR above.

github-actions[bot] commented 3 years ago

:tada: This issue has been resolved in version 1.1.3 :tada:

The release is available on:

Your semantic-release bot :package::rocket: