Skip to content
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

[BUG] Breaking change from @types/react@18.2.61 #1045

Closed
Xavier59 opened this issue Mar 6, 2024 · 6 comments
Closed

[BUG] Breaking change from @types/react@18.2.61 #1045

Xavier59 opened this issue Mar 6, 2024 · 6 comments
Labels
bug Something isn't working released

Comments

@Xavier59
Copy link

Xavier59 commented Mar 6, 2024

Describe the bug
DefinitelyTyped/DefinitelyTyped#68720 has caused a breaking change.

Reproduction

const components: TableComponents<DataType, Context> = {
	Scroller: forwardRef<HTMLDivElement>((props, ref) => <MUITableContainer {...props} ref={ref} />),
	TableRow: ({ item, ...props }: { item: DataType }) => <StyledRow {...props} />,
	Table: props => <Table {...props} style={{ borderCollapse: 'separate', overflowAnchor: 'none' }} />,
	TableHead,
	EmptyPlaceholder: ({ context }: { context?: Context }) => (
		<TableBody>
			<NoDataRow>{emptyState?.(context)}</NoDataRow>
		</TableBody>
	),
	TableBody: forwardRef<HTMLTableSectionElement>((props, ref) => <TableBody {...props} ref={ref} />),
};

Scroller and TableBody are not the right type.

Type 'ForwardRefExoticComponent<RefAttributes<HTMLDivElement>>' is not assignable to type 'ComponentType<Pick<Omit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & { ...; }, "style" | ... 1 more ... | "children"> & { ...; } & { ...; }> | undefined'.
  Type 'ForwardRefExoticComponent<RefAttributes<HTMLDivElement>>' is not assignable to type 'FunctionComponent<Pick<Omit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & { ...; }, "style" | ... 1 more ... | "children"> & { ...; } & { ...; }>'.
    Types of property 'propTypes' are incompatible.
      Type 'WeakValidationMap<RefAttributes<HTMLDivElement>> | undefined' is not assignable to type 'WeakValidationMap<Pick<Omit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & { ...; }, "style" | ... 1 more ... | "children"> & { ...; } & { ...; }> | undefined'.
        Type 'WeakValidationMap<RefAttributes<HTMLDivElement>>' is not assignable to type 'WeakValidationMap<Pick<Omit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & { ...; }, "style" | ... 1 more ... | "children"> & { ...; } & { ...; }>'.
          Types of property 'ref' are incompatible.
            Type 'Validator<LegacyRef<HTMLDivElement> | undefined> | undefined' is not assignable to type 'Validator<string | Pick<Omit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & { ...; }, "ref">> | undefined'.
              Type 'Validator<LegacyRef<HTMLDivElement> | undefined>' is not assignable to type 'Validator<string | Pick<Omit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & { ...; }, "ref">>'.
                Type 'LegacyRef<HTMLDivElement> | undefined' is not assignable to type 'string | Pick<Omit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & { ...; }, "ref">'.
                  Type 'undefined' is not assignable to type 'string | Pick<Omit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & { ...; }, "ref">'.ts(2322)
index.d.ts(492, 5): The expected type comes from property 'Scroller' which is declared here on type 'TableComponents<DataType, Context>'

Expected behavior
No typescript error

@Xavier59 Xavier59 added the bug Something isn't working label Mar 6, 2024
@mrlika
Copy link

mrlika commented Mar 7, 2024

A similar issue with @types/react@18.2.61

This component is not assignable to components.List any more:

const EmployeesList = forwardRef<HTMLDivElement, ListProps>(({ children, ...restProps }, ref) => (
  <div className='employees-list' {...restProps} ref={ref}>
    {children}
  </div>
));

<Virtuoso
  className='grid-table-body scrollbar-light-theme'
  data={sortedEmployeesList}
  components={{ List: EmployeesList }}
  overscan={500}
  itemContent={(_, employee) => renderEmployeeRow(employee)}
/>

With the TypeScript error:

Type 'ForwardRefExoticComponent<Omit<ListProps, "ref"> & RefAttributes<HTMLDivElement>>' is not assignable to type 'ComponentType<Pick<Omit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & { ...; }, "ref" | ... 1 more ... | "children"> & { ...; } & { ...; }> | undefined'.
  Type 'ForwardRefExoticComponent<Omit<ListProps, "ref"> & RefAttributes<HTMLDivElement>>' is not assignable to type 'FunctionComponent<Pick<Omit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & { ...; }, "ref" | ... 1 more ... | "children"> & { ...; } & { ...; }>'.
    Types of property 'propTypes' are incompatible.
      Type 'WeakValidationMap<Omit<ListProps, "ref"> & RefAttributes<HTMLDivElement>> | undefined' is not assignable to type 'WeakValidationMap<Pick<Omit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & { ...; }, "ref" | ... 1 more ... | "children"> & { ...; } & { ...; }> | undefined'.
        Type 'WeakValidationMap<Omit<ListProps, "ref"> & RefAttributes<HTMLDivElement>>' is not assignable to type 'WeakValidationMap<Pick<Omit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & { ...; }, "ref" | ... 1 more ... | "children"> & { ...; } & { ...; }>'.
          Types of property 'ref' are incompatible.
            Type 'Validator<LegacyRef<HTMLDivElement> | undefined> | undefined' is not assignable to type 'Validator<((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement> | null | undefined> | undefined'.
              Type 'Validator<LegacyRef<HTMLDivElement> | undefined>' is not assignable to type 'Validator<((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement> | null | undefined>'.
                Type 'LegacyRef<HTMLDivElement> | undefined' is not assignable to type '((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement> | null | undefined'.
                  Type 'string' is not assignable to type '((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement> | null | undefined'.ts(2322)

@petyosi
Copy link
Owner

petyosi commented Mar 7, 2024

I'll have to check what the author did as a migration. @mrlika @Xavier59 - happy to accept a PR if you have the capacity, should be a small change I hope.

@petyosi petyosi closed this as completed in c5c6dbd Mar 8, 2024
Copy link

github-actions bot commented Mar 8, 2024

🎉 This issue has been resolved in version 4.7.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@fdw
Copy link

fdw commented Mar 27, 2024

I still see a similar error with react-virtuoso: 4.7.2 and @types/react: 18.2.65:

const VirtuosoTableComponents: WithRequired<TableComponents, 'Scroller' | 'TableBody'> = {
  TableFoot: TableFooter,
  Scroller: forwardRef((props, ref) => <TableContainer component={Paper} {...props} ref={ref} />),
  Table: Table,
  TableHead: TableHead,
  TableRow: TableRow,
  TableBody: forwardRef((props, ref) => <TableBody {...props} ref={ref} />)
}

with export type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] }

src/components/common/table/VirtuosoTableComponents.tsx:14:3 - error TS2322: Type 'OverridableComponent<TableFooterTypeMap<{}, "tfoot">>' is not assignable to type 'ComponentType<Pick<DetailedHTMLProps<HTMLAttributes<HTMLTableSectionElement>, HTMLTableSectionElement>, "style" | "children"> & { ...; } & RefAttributes<...>> | undefined'.
  Type 'OverridableComponent<TableFooterTypeMap<{}, "tfoot">>' is not assignable to type 'FunctionComponent<Pick<DetailedHTMLProps<HTMLAttributes<HTMLTableSectionElement>, HTMLTableSectionElement>, "style" | "children"> & { ...; } & RefAttributes<...>>'.
    Types of parameters 'props' and 'props' are incompatible.
      Type 'Pick<DetailedHTMLProps<HTMLAttributes<HTMLTableSectionElement>, HTMLTableSectionElement>, "style" | "children"> & { ...; } & RefAttributes<...>' is not assignable to type '{ component: any; } & TableFooterOwnProps & CommonProps & Omit<any, "className" | "style" | "classes" | "children" | "sx">'.
        Property 'component' is missing in type 'Pick<DetailedHTMLProps<HTMLAttributes<HTMLTableSectionElement>, HTMLTableSectionElement>, "style" | "children"> & { ...; } & RefAttributes<...>' but required in type '{ component: any; }'.

14   TableFoot: TableFooter,
     ~~~~~~~~~

  node_modules/@mui/material/OverridableComponent.d.ts:21:7
    21       component: RootComponent;
             ~~~~~~~~~
    'component' is declared here.
  node_modules/react-virtuoso/dist/index.d.ts:478:5
    478     TableFoot?: React_2.ComponentType<Pick<React_2.ComponentProps<'tfoot'>, 'style' | 'children'> & {
            ~~~~~~~~~
    The expected type comes from property 'TableFoot' which is declared here on type 'WithRequired<TableComponents<unknown, unknown>, "Scroller" | "TableBody">'

Am I doing something wrong?

@chitoku-k
Copy link

@fdw
You can circumvent that with this way:

import type { ComponentPropsWithoutRef } from 'react'
import { forwardRef } from 'react'
import type { TableComponents } from 'react-virtuoso'
import Paper from '@mui/material/Paper'
import Table from '@mui/material/Table'
import TableBody from '@mui/material/TableBody'
import TableContainer from '@mui/material/TableContainer'
import TableFooter from '@mui/material/TableFooter'
import TableHead from '@mui/material/TableHead'
import TableRow from '@mui/material/TableRow'

const VirtuosoTableComponents: TableComponents = {
  TableFoot: forwardRef((props: ComponentPropsWithoutRef<'tfoot'>, ref) => (
    <TableFooter ref={ref} {...props} />
  )),
  Scroller: forwardRef((props: ComponentPropsWithoutRef<'div'>, ref) => (
    <TableContainer ref={ref} component={Paper} {...props} />
  )),
  Table,
  TableHead: forwardRef((props: ComponentPropsWithoutRef<'thead'>, ref) => (
    <TableHead ref={ref} {...props} />
  )),
  TableRow,
  TableBody: forwardRef((props: ComponentPropsWithoutRef<'tbody'>, ref) => (
    <TableBody ref={ref} {...props} />
  )),
}

@fdw
Copy link

fdw commented Mar 27, 2024

That was quick! Thank you! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

No branches or pull requests

5 participants