react-table-sticky icon indicating copy to clipboard operation
react-table-sticky copied to clipboard

'sticky' does not exist in type 'Column<T>'

Open Newbie012 opened this issue 4 years ago • 2 comments

I have a basic Table component:

import React from "react";
import { useTable, UseTableOptions } from "react-table";
import { useSticky } from "react-table-sticky";

export type TableProps<D extends object = {}> = UseTableOptions<D>;

const Table = <D extends object>(props: TableProps<D>) => {
  const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } = useTable(props, useSticky);

  return (
    <table {...getTableProps()}>
      ...
    </table>
  );
};

export default Table;

And this is how I call the component:

<Table columns={columns} data={data}></Table>

The thing is, when I try to statically declare the columns, I get the following error:

type '{ id: string; Header: string; sticky: string; columns: ({ Header: string; accessor: "name"; } | { Header: string; accessor: "status"; } | { Header: string; accessor: "createdAt"; })[]; }' is not assignable to type 'Column<AdvertiserColumns>'.
  Object literal may only specify known properties, and 'sticky' does not exist in type 'Column<AdvertiserColumns>'.ts(2322)

image

Which is correct, because in the end it points to this interface:

export interface UseTableColumnOptions<D extends object> {
    id?: IdType<D>;
    Header?: Renderer<HeaderProps<D>>;
    width?: number | string;
    minWidth?: number;
    maxWidth?: number;
}

I know I can simply remove the typing Column<AdvertiserColumns>[], but then it might lead to some bugs (e.g write header instead of Header). Any ideas?

Newbie012 avatar Jan 22 '21 09:01 Newbie012

How about Column<AdvertiserColumns & { sticky: "right" | "left" }>[]

franklinharvey avatar Mar 22 '22 23:03 franklinharvey

solved this by updating the react-table-config.d.ts file from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react-table

declare module 'react-table' {
  export type UseStickyOptions = Partial<{
    sticky: 'left' | 'right';
  }>;

  ...

  export interface ColumnInterface<
    D extends Record<string, unknown> = Record<string, unknown>
  > extends UseFiltersColumnOptions<D>,
      UseGlobalFiltersColumnOptions<D>,
      UseGroupByColumnOptions<D>,
      UseResizeColumnsColumnOptions<D>,
      UseSortByColumnOptions<D>,
      UseStickyOptions {} // add this line

  ...
}

monoppa avatar May 04 '23 08:05 monoppa