ColumnDef types gives typescript error
See original GitHub issueDescribe 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],
)
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
- Create a type with two different types, string and name for example.
- Use the createColumnHelper with the type
- 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
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:
- Created a year ago
- Reactions:5
- Comments:15 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Pre-typing would be helpful for public interfaces e.g. if we create a wrapper components with
columnsanddataprops. I am using this here right now:That makes TypeScript “happy”… but I would prefer something more strict.
I guess I was looking for a typed
CustomTableComponentexample which uses TanStack/Table underneath.@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?