table: ColumnDef types gives typescript error
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],
)
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.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 74
- Comments: 61 (2 by maintainers)
Commits related to this issue
- docs: Remove extra typing for Column Defs When using an `accessor` type column helper in a top level column defs array where the array is also typed, you get the type error mentioned in #4382. It was... — committed to csandman/table by csandman 4 years ago
- docs: Remove extra typing for Column Defs when using helper (#4430) When using an `accessor` type column helper in a top level column defs array where the array is also typed, you get the type error ... — committed to TanStack/table by csandman 4 years ago
Getting the same here, trying to convince our team to migrate to @tanstack/react-table and this is certainly getting in the way of that. I’ve defined columns as such: (nothing revolutionary)
and have typed my generic table component as:
But am then getting the following Type Error when passing my columns as a prop:
Any advice is appreciated
react-table version: 8.10.7 @types/react-table version: 7.7.18
Hey @tannerlinsley 👋 I there any update on “
any” type issue? It seems this issue is dead without being resolved for long time than I would expect. Should I open separated issue instead? We really love this library but having proper type instead ofanywould really help us.Still facing this issue
A common but subtly incorrect approach^1 to define the component properties of a table is:
When using
createColumnHelper, sinceTValuedepends on which column is being accessed,columns: ColumnDef<TData, TValue>[]is not specific enough to satisfy the constraints imposed bycreateColumnHelper.The following approach passes typechecking without the use of
any:It’s not particularly readable, but it will satisfy your linter!
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.I am having the same issue here. Is there a way to avoid typing
any?It becomes an issue when passing
columnsto a component as a prop.We skipped using columnDef, it still works as good and gives type help.
Any updates? Two years have already passed
Bump.
You should only be using the column helper and not pre-typing anything.
I thought I was going crazy, followed the docs to a T, defined some straightforward columns, but can’t make a generic table component and pass the columns to them with type safety for the life of me. Glad to see I’m not alone I guess.
This! We’re making a custom
MyTableComponentand exposecolumnsas prop that is pre-typed.This is the way.
Fix mentioned by @jack-davies works almost all the time. The only problem is data with optional fields.
Adding
Requiredfixes it.It removes
undefinedfromDataTableProps['columns']type which finally satisfiesColumnDef<TData, any>[]fromTableOptions<TData>['columns']Hello, I am still facing the same issue on
8.15.0and8.15.3. I am following the shadcn example (Preview / Code) Can’t make it work withnor
I am still getting the following error:
The only workaround I use for now is:
still waiting for the official fix lol
@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?
Look this https://reactdev.space/questions/using-tanstack-query-along-with-the-useeffect-hook-allows-for-efficient way for the answer
If you are still looking for a solution I managed to dodge the typescript type issue by exporting the columns like this:
columns.tsx
This way you are not loosing types when defining the columns.
I solved it by using a type assertion.
It may not be a proper solution, but it works for me.
That’s very excessive, just put any for the second argument and it’ll still work really well.
Waiting for the solution to this issue too.
+1
Only way I’ve found to get around this is to add a
as stringor some other primitive type afterinfo.getValue()Any updates how to fix correct?
@4ndrs That’s what I did previously, it’s not ideal but it work’s perfectly fine tbh.
This is my demo from what I created a tanstack table at my last role, see this
I wish people would stop spamming this thread 😆 There a good few answers here now, give them a go and see if any of them help you out.
@tannerlinsley and all the other maintainers/typescript wizards have done an incredible job with this library especially after it got a massive upgrade in ❤️ It’s really difficult to get this sort stuff right so I’m sure in this particular case an “any” or “unknown” cast will do the trick everything else works great last time I checked.
I’m going to unsubscribe from this thread as I keep getting emails about it 🤣
@RestartDK
I have been using the
anyapproach when defining my table props:I haven’t had any issues with inference or missing types so far.
@tanstack/react-table: “^8.15.3”
I had something similar to this when I tried to pass the
columnsand define them between componentsFor some reason it worked with me when I changed the type that will be used on the
ColumnDeffrom being defined byinterfaceto be defined by type, for example:From
To
And I have no idea of why and how
Yeah… we should fix that. On Oct 5, 2022 at 11:40 AM -0700, Chris Sandvik @.***>, wrote:
@tannerlinsley This is the first example in the Column Defs guide page:
It has the column array pre-typed and throws errors if any of these accessor columns are top level.
I think I’ve managed to fix this without falling back to
anyby modifying theColumnHelpertype! PR here.Hey, I was facing the exact same issue but since I use the 8.21.2 version and type my
dataandcolumnsprops with this type definition, the error is gone.I’m maybe missing something but it can maybe help some of you
This is what I ended up doing:
whatever is the type coming from
dataandcolumnsarray, are what my component will expect to receive.columnsis still of typeColumnDef<TData, any>[]and it’s fine for me.I am getting the below error while using like this. Type ‘ColumnDef<unknown>[]’ is not assignable to type ‘never’.
👀
This still happens only when you have multiple types on your object property
Any update on this issue? Running into the same issue. My solution is to use any for the type and individually define the types for each cell:
Same use case as above, useTable is wrapped in a reuseable component that takes in
columnsas a prop. Trying to use columHelper to create the definitions in the parent and it causes a typescript error.I just encountered the same issue, which forced me to turn off
no-explicit-any.