ColumnDef types gives typescript error

See original GitHub issue

Describe the bug

Looking at the examples and docs I expect this to correctly type my columns for me. Instead I get a large error with this code:

  type Example = {
    name: string
    age: number
  }

  const columnHelper = createColumnHelper<Example>()

  const columns = useMemo<ColumnDef<Example>[]>(
    () => [
      columnHelper.accessor("name", {
        cell: (info) => info.getValue(),
      }),
      columnHelper.accessor("age", {
        cell: (info) => info.getValue(),
      }),
    ],
    [columnHelper],
  )
bild

Am I doing something wrong here?

Regards Jonathan

Your minimal, reproducible example

https://codesandbox.io/s/typescript-playground-export-forked-iqm265?file=/index.tsx

Steps to reproduce

  1. Create a type with two different types, string and name for example.
  2. Use the createColumnHelper with the type
  3. Define columns with columnHelper and type them with ColumnDef<type>

Expected behavior

I expected the Columdef to correctly type my columns.

How often does this bug happen?

Every time

Screenshots or Videos

bild

Platform

Mac OS

react-table version

v8.5.13

TypeScript version

v4.8.2

Additional context

No response

Terms & Code of Conduct

  • I agree to follow this project’s Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:5
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
swernerxcommented, Oct 18, 2022

Pre-typing would be helpful for public interfaces e.g. if we create a wrapper components with columns and data props. I am using this here right now:

interface DataTableProps {
  // FIXME: Can we figure out something more type restrictive which actually works?
  data: unknown[]
  columns: ColumnDef<any, any>[];
}

That makes TypeScript “happy”… but I would prefer something more strict.

I guess I was looking for a typed CustomTableComponent example which uses TanStack/Table underneath.

3reactions
csandmancommented, Oct 4, 2022

@tannerlinsley if you’re saying @Jontii 's solution is the right way, you might want to change the Column Defs page in the docs to not use a column helper and a typed array. That is what you’re saying right, either use one or the other?

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Table Typescript "Type is not assignable" - Stack Overflow
I get an error when doing const tableInstance = useTable({ columns, data }) . Type '{ Header: string; accessor: string; }[]' is not...
Read more >
Column Definitions - JavaScript Data Grid
Each column in the grid is defined using a Column Definition ( ColDef ). Columns are positioned in the grid according to the...
Read more >
Create a generic table with React and Typescript
It takes in columns as props, and the type definition is the same as for our ... Typescript will give you an error...
Read more >
Type 'unknown' is not assignable to type in TypeScript
To solve the error, use a type assertion or a type guard to verify that ... In the if blocks, give us support...
Read more >
Safer ag-Grid (React) Column Definitions with TypeScript
This is not required but ag-Grid comes with type definitions and these make development a lot easier. Because columnDefs is declared as an ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found