Typescript Discussion
See original GitHub issueHi all,
I’d like to start by saying thank you to all the maintainers and contributors of this package 💯 and also apologies for not using the issue template format, wasn’t quite sure if this was a bug or a feature request.
I’ve been trialling the v7 branch, and overall its really really impressive 🙌 The only part that seemed a little … lacking was the Typescript types. This was not a huge issue, as I was able to create my own types file that the compiler merges with the types provided by react-table
(using declaration merging)
I noticed that beta 12 has recently been released, and I updated (from beta 10). Unfortunately, it seems that the situation has somewhat worsened from a developer experience perspective. I’m assuming the changes were made in order to better handle the fact that any number of plugin hooks might be specified by a user. However, these changes seem to have come at the cost of requiring users to include a custom types file in their projects (which is a little unusual, even for Typescript projects).
In an ideal world, it’d be great to see react-table
written natively in Typescript to avoid situations where the code and types are not in sync, but I do understand that might not be what the maintainers/contributors want. That said, the procedure in beta 12 seems like it could be improved, and I’m happy to help 😄 I’m including my additions to the types file from beta 10 that mostly fixed the plugin issues, as well as adding a few other missing types. There are some definite improvements in the beta 12 types, so it’d be good to see if we can find some middle ground.
import {
Row,
TableInstance,
TableOptions,
useGroupBy,
useFilters,
useSortBy,
useExpanded,
usePagination,
useRowSelect,
useRowState,
useColumnOrder,
useBlockLayout,
useAbsoluteLayout,
useResizeColumns,
Column,
} from "react-table";
declare module 'react-table' {
export interface TableInstance<D = {}> extends TableOptions<D> {
getTableBodyProps: (userProps?: any) => any
selectedFlatRows: Row<D>[]
}
export interface HeaderColumn<D> {
disableFilters?: boolean;
}
export interface EnhancedColumn<D>
extends Omit<Column<D>, 'columns'>,
TableInstance<D> {
canFilter?: boolean
}
export interface UsePaginationValues<D = {}> {
pageOptions: number[]
}
export type Plugin<D> = useGroupBy<D> |
useFilters<D> |
useSortBy<D> |
useExpanded<D> |
usePagination<D> |
useRowSelect<D> |
useRowState<D> |
useColumnOrder<D> |
useBlockLayout<D> |
useAbsoluteLayout<D> |
useResizeColumns<D>
export function useTable<D = {}, P = Plugin<D>>(
props: TableOptions<D>,
...plugins: P[]
): TableInstance<D> & ReturnType<typeof P>
export function useRowSelect<D = {}>(
props: TableOptions<D>
): TableOptions<D>
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:18
- Comments:66 (14 by maintainers)
I hear you, and I even tried to do that at the start of 2020. However, I quickly learned a few things:
any
s andunknown
s. This is fine, but not in the long run. At some point, the work/time will be better spent in a rewrite.With all of that said, you’ll be happy to know that I’m currently writing v8 natively in TypeScript. I assure you, it’s a beast, but it’s also awesome. Take it from me, with what I know about the internals and current API in v7, there’s no way to backport accurate types to v7 that will even come close to v8.
Still working actively on it.
Tanner Linsley On Jul 20, 2021, 8:22 AM -0600, Nubami SQReder @.***>, wrote: