使用打字稿反应表出现问题

Qua*_*Đàm 6 typescript reactjs react-table

使用带有 useGlobalFilter 的 react-table 时出现打字稿错误。我只是按照互联网上的一些说明进行操作。这是我的代码:

const DataTable : React.FC<IDataTableProps> = ({columns, data}) => {
    const {
        getTableProps,
        getTableBodyProps,
        headerGroups,
        rows,
        prepareRow,
        setGlobalFilter,
        state,
    } = useTable({columns, data}, useGlobalFilter);

    const GlobalFilter = ({ globalFilter , setGlobalFilter} : {globalFilter: any, setGlobalFilter: any})  => {
        return (
            <input
                value={globalFilter || ""}
                onChange={e => {
                setGlobalFilter(e.target.value || undefined); // Set undefined to remove the filter entirely
                }}
                placeholder={`Search All ...`}
            />
            );
        };

    return <Container>
        <GlobalFilter globalFilter={state.globalFilter} setGlobalFilter={setGlobalFilter} />
        <Table {...getTableProps()}>
            <THead>
                {headerGroups.map(headerGroup => (
                    <TR {...headerGroup.getHeaderGroupProps()}>
                        {headerGroup.headers.map(column => {
                            return (
                                <TH {...column.getHeaderProps()}>{column.render("Header")}</TH>
                            )}
                        )}
                    </TR>
                ))}
            </THead>
            <TBody {...getTableBodyProps()} >
                {rows.map((row, i) => {
                    prepareRow(row);
                    return (
                        <TR {...row.getRowProps()}>
                            {row.cells.map(cell => {
                                return <TD {...cell.getCellProps()} >
                                    {cell.render("Cell")}
                                </TD>
                            })}
                        </TR>
                    )
                })}
            </TBody>
        </Table>
    </Container>
}
Run Code Online (Sandbox Code Playgroud)

我得到的打字稿错误:“TableInstance”类型上不存在“setGlobalFilter”属性。TS2339

谁能帮我?非常感谢!

小智 10

您可能丢失了@types/react-table包裹。

如果已经安装了该软件包,则需要执行另一个步骤才能运行它。您需要react-table-config.d.ts在文件夹中创建一个文件@types

请填写以下配置,去掉不需要的部分

import {
  UseColumnOrderInstanceProps,
  UseColumnOrderState,
  UseExpandedHooks,
  UseExpandedInstanceProps,
  UseExpandedOptions,
  UseExpandedRowProps,
  UseExpandedState,
  UseFiltersColumnOptions,
  UseFiltersColumnProps,
  UseFiltersInstanceProps,
  UseFiltersOptions,
  UseFiltersState,
  UseGlobalFiltersColumnOptions,
  UseGlobalFiltersInstanceProps,
  UseGlobalFiltersOptions,
  UseGlobalFiltersState,
  UseGroupByCellProps,
  UseGroupByColumnOptions,
  UseGroupByColumnProps,
  UseGroupByHooks,
  UseGroupByInstanceProps,
  UseGroupByOptions,
  UseGroupByRowProps,
  UseGroupByState,
  UsePaginationInstanceProps,
  UsePaginationOptions,
  UsePaginationState,
  UseResizeColumnsColumnOptions,
  UseResizeColumnsColumnProps,
  UseResizeColumnsOptions,
  UseResizeColumnsState,
  UseRowSelectHooks,
  UseRowSelectInstanceProps,
  UseRowSelectOptions,
  UseRowSelectRowProps,
  UseRowSelectState,
  UseRowStateCellProps,
  UseRowStateInstanceProps,
  UseRowStateOptions,
  UseRowStateRowProps,
  UseRowStateState,
  UseSortByColumnOptions,
  UseSortByColumnProps,
  UseSortByHooks,
  UseSortByInstanceProps,
  UseSortByOptions,
  UseSortByState
} from 'react-table'

declare module 'react-table' {
  // take this file as-is, or comment out the sections that don't apply to your plugin configuration

  export interface TableOptions<D extends Record<string, unknown>>
    extends UseExpandedOptions<D>,
      UseFiltersOptions<D>,
      UseGlobalFiltersOptions<D>,
      UseGroupByOptions<D>,
      UsePaginationOptions<D>,
      UseResizeColumnsOptions<D>,
      UseRowSelectOptions<D>,
      UseRowStateOptions<D>,
      UseSortByOptions<D>,
      // note that having Record here allows you to add anything to the options, this matches the spirit of the
      // underlying js library, but might be cleaner if it's replaced by a more specific type that matches your
      // feature set, this is a safe default.
      Record<string, any> {}

  export interface Hooks<D extends Record<string, unknown> = Record<string, unknown>>
    extends UseExpandedHooks<D>,
      UseGroupByHooks<D>,
      UseRowSelectHooks<D>,
      UseSortByHooks<D> {}

  export interface TableInstance<D extends Record<string, unknown> = Record<string, unknown>>
    extends UseColumnOrderInstanceProps<D>,
      UseExpandedInstanceProps<D>,
      UseFiltersInstanceProps<D>,
      UseGlobalFiltersInstanceProps<D>,
      UseGroupByInstanceProps<D>,
      UsePaginationInstanceProps<D>,
      UseRowSelectInstanceProps<D>,
      UseRowStateInstanceProps<D>,
      UseSortByInstanceProps<D> {}

  export interface TableState<D extends Record<string, unknown> = Record<string, unknown>>
    extends UseColumnOrderState<D>,
      UseExpandedState<D>,
      UseFiltersState<D>,
      UseGlobalFiltersState<D>,
      UseGroupByState<D>,
      UsePaginationState<D>,
      UseResizeColumnsState<D>,
      UseRowSelectState<D>,
      UseRowStateState<D>,
      UseSortByState<D> {}

  export interface ColumnInterface<D extends Record<string, unknown> = Record<string, unknown>>
    extends UseFiltersColumnOptions<D>,
      UseGlobalFiltersColumnOptions<D>,
      UseGroupByColumnOptions<D>,
      UseResizeColumnsColumnOptions<D>,
      UseSortByColumnOptions<D> {}

  export interface ColumnInstance<D extends Record<string, unknown> = Record<string, unknown>>
    extends UseFiltersColumnProps<D>,
      UseGroupByColumnProps<D>,
      UseResizeColumnsColumnProps<D>,
      UseSortByColumnProps<D> {}

  export interface Cell<D extends Record<string, unknown> = Record<string, unknown>, V = any>
    extends UseGroupByCellProps<D>,
      UseRowStateCellProps<D> {}

  export interface Row<D extends Record<string, unknown> = Record<string, unknown>>
    extends UseExpandedRowProps<D>,
      UseGroupByRowProps<D>,
      UseRowSelectRowProps<D>,
      UseRowStateRowProps<D> {}
}
Run Code Online (Sandbox Code Playgroud)

解决方案来自React-Table的DefinitelyTyped

  • 在@types 文件夹中声明react-table-config.d.ts 文件后,我收到错误。有什么解决方法吗?``(错误:'TableOptions' 的所有声明必须具有相同的类型参数。ts(2428))`` (6认同)

lau*_*erm 8

如果您在从DefinelyTypedAll declarations of 'TableInstance' must have identical type parameters.ts(2428)进行迁移指南时收到投诉,您可以手动调整提供的文件以与包中的类型参数匹配。react-table-config.d.ts@types/react-table

转到类型定义包(node_modules/@types/react-table),查看index.d.ts文件,您会看到许多通用接口,这些接口具有与您react-table-config.d.ts文件中的接口不同的参数,这就是为什么它在尝试合并这些声明时会抱怨。

例如,这是类型定义包中的接口

export interface TableInstance<D extends object = {}>
Run Code Online (Sandbox Code Playgroud)

这就是你的相应界面react-table-config.d.ts

export interface TableInstance<D extends Record<string, unknown> = Record<string, unknown>>
Run Code Online (Sandbox Code Playgroud)

从那里开始,修复应该很简单,只需更改react-table-config.d.ts文件中的参数部分以与您的类型定义包匹配即可。

Alterreact-table-config.d.ts并不是真正理想的(尤其是“按原样获取此文件”评论),但这仍然是最适合我的方法,没有任何抱怨,也没有降级(我正在使用react-table7.7.0 和@types/react-table7.7.2)。我希望react-tablev8 能够有内置的类型定义来避免这个令人头疼的问题。