table icon indicating copy to clipboard operation
table copied to clipboard

Nested types cause TS "Type instantiation is excessively deep and possibly infinite." error on columnHelper accessor

Open mogzol opened this issue 1 year ago • 16 comments

Describe the bug

Trying to create a column definition through createColumnHelper with nested, repeated types causes the typescript error "Type instantiation is excessively deep and possibly infinite."

Your minimal, reproducible example

https://codesandbox.io/s/tanstack-table-nested-column-issue-j912po

Steps to reproduce

  1. Create a new typescript file with these contents:
import { createColumnHelper } from "@tanstack/table-core";

interface Data {
  name: string;
  nested: Data;
}

const columnHelper = createColumnHelper<Data>();
columnHelper.accessor("name", {});
  1. Observe the typescript error on the last line.

Expected behavior

There should not be a typescript error

How often does this bug happen?

No response

Screenshots or Videos

No response

Platform

  • macOS 12.5.1, node 16

react-table version

8.5.13

TypeScript version

4.8.3

Additional context

Weirdly this doesn't happen when creating a column definition directly, this works fine without errors:

const column: ColumnDef<Data> = {
  id: "name",
  accessorKey: "name",
};

It's just when trying to use createColumnHelper that errors occur.

Terms & Code of Conduct

  • [X] I agree to follow this project's Code of Conduct
  • [X] 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.

mogzol avatar Sep 15 '22 22:09 mogzol

Just noticed that #4224 is likely the same issue, however it has been marked as fixed and closed, but this is still happening on the latest TanStack table version.

mogzol avatar Sep 16 '22 17:09 mogzol

Still happenning, just tried with [email protected] version

apperside avatar Oct 03 '22 11:10 apperside

Still happenning, just tried with [email protected] version

If you have some spare time, would you consider pulling down this PR, building and linking. That PR eliminates a duplicated call to a potentially expensive recursive type, as well as enabling type caching wherever possible.

terry-au avatar Oct 04 '22 13:10 terry-au

@terry-au I had the same use case and issue as described by @mogzol, a recursive type that causes the excessively deep error, and tested your PR (built and linked) -- no success, same issue.

For now, I'm using Pick<OriginalType, "field1" | "field2" | "etc">, which does get around these issues (as I'm excluding the fields that may be recursive), though not the most ideal.

lrstanley avatar Oct 10 '22 02:10 lrstanley

This also happens if you use something like HTMLElement or ReactNode. I'm new to TanStack, and I can't seem to get anything working besides string, number and LocalDate, otherwise I get the excessively deep error.

SRotendahl avatar Oct 12 '22 08:10 SRotendahl

This also happens if you use something like HTMLElement or ReactNode. I'm new to TanStack, and I can't seem to get anything working besides string, number and LocalDate, otherwise I get the excessively deep error.

Have you tried using ColumnDef<YourType> directly, rather than using the column-helper methods/functions? As I don't believe this would occur under that scenario (or if it does, should be much less likely).

lrstanley avatar Oct 12 '22 16:10 lrstanley

We also get "Type instantiation is excessively deep and possibly infinite." for createColumnHelper on our github actions after upgrading to typescript 4.8.4 with tanstack/[email protected]. Using ColumnDef works for us though.

khnn avatar Oct 12 '22 20:10 khnn

That seems to work, I had tried using ColumnDef before but I had defined it incorrectly, it seems to work now. Thank you :)

SRotendahl avatar Oct 13 '22 09:10 SRotendahl

@terry-au I had the same use case and issue as described by @mogzol, a recursive type that causes the excessively deep error, and tested your PR (built and linked) -- no success, same issue.

For now, I'm using Pick<OriginalType, "field1" | "field2" | "etc">, which does get around these issues (as I'm excluding the fields that may be recursive), though not the most ideal.

@lrstanley @SRotendahl I have fixed this issue and updated my PR. Column helper should now only do the bare minimum of recursion needed to infer a type at a given path.

image

terry-au avatar Oct 18 '22 00:10 terry-au

How to fix that? Still facing the same issue in react-table:8.7.4

its-monotype avatar Jan 05 '23 23:01 its-monotype

How to fix that? Still facing the same issue in react-table:8.7.4

You can place // @ts-ignore above columnHelper. But better not to use columnHelper

Instead, you can define columns as in the example below. https://tanstack.com/table/v8/docs/examples/react/column-sizing

const defaultColumns: ColumnDef<Person>[] = [
  {
    header: 'Name',
    footer: props => props.column.id,
    columns: [
      {
        accessorKey: 'firstName',
        cell: info => info.getValue(),
        footer: props => props.column.id,
      },
      {
        accessorFn: row => row.lastName,
        id: 'lastName',
        cell: info => info.getValue(),
        header: () => <span>Last Name</span>,
        footer: props => props.column.id,
      },
    ],
  }
]

unifam123 avatar Mar 18 '23 14:03 unifam123

Running into this issue on a project with latest version (8.9.3).

Edit: My repro is basically the same as the one in description 😅

Not sure if one has already been shared, but here's a very simple repro of the problem based on the following code:

import { createColumnHelper } from "@tanstack/react-table";

interface Person {
  name: string;
  age: number;
  parent: Person;
}

const columnHelper = createColumnHelper<Person>();

export const columns = [columnHelper.accessor("name")];

breadadams avatar Jul 07 '23 16:07 breadadams

I just use an accessorFn instead of accessorKey when that happens. Recently, mongoose _id Object id types cause the issue for me

KevinVandy avatar Jul 07 '23 20:07 KevinVandy

I have the exact same problem and it messes up intellisense for my whole project...

I don't want to use ColumnDef - while it does work, it's not as type safe as columnHelper which is why I used it in the first place. For example, using ColumnDef and defining a cell function, the value from getValue is not typed

oren-legit avatar Jul 23 '23 11:07 oren-legit

This is still happening for me, when I use columnHelper, it messes up the whole intellisense of vscode

sohhamm avatar Aug 09 '23 07:08 sohhamm

Any ideas on ovverride?

yarinsa avatar Oct 05 '23 17:10 yarinsa