TanStack / TanStack/form

Type instantiation is excessively deep and possibly infinite with recursive field value structure

Open
#1,484 1 comment 11 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area: types type: enhancement v1 v2
Dominant language
TypeScript
Stars
6.7k
Forks
682
Avg merge
5d 18h
Merged PRs (30d)
7

Description

Describe the bug

We are trying to adopt tanstack form in our CMS, where we have complex and recursive content structure, and in the form types we represent the field values as:

export type FieldValues = {
  [key: string]: string | string[] | number | boolean | null | FieldValues | FieldValues[];
};

Also, we are passing down the form instance via a provider as our codebase is huge and we need a context to share the form instance deep down the tree.

type FormContextType = ReactFormExtendedApi<
  FieldValues,
  FormValidateOrFn<FieldValues> | undefined,
  ZodType<FieldValues, ZodTypeDef, FieldValues>,
  FormAsyncValidateOrFn<FieldValues> | undefined,
  FormValidateOrFn<FieldValues> | undefined,
  FormAsyncValidateOrFn<FieldValues> | undefined,
  FormValidateOrFn<FieldValues> | undefined,
  FormAsyncValidateOrFn<FieldValues> | undefined,
  FormAsyncValidateOrFn<FieldValues> | undefined,
  {}
> | null;

export const FormContext = React.createContext<FormContextType>(null);

export const FormProvider = ({
  children,
  value,
}: {
  children: React.ReactNode;
  value: FormContextType;
}) => {
  return <FormContext.Provider value={value}>{children}</FormContext.Provider>;
};

function App() {
  const form = useForm({
    defaultValues: defaultPeople,
    onSubmit({ value }) {
      alert(JSON.stringify(value));
    },
  });

  return (
    <FormProvider value={form}>
      <form
        onSubmit={(e) => {
          e.preventDefault();
          e.stopPropagation();
          form.handleSubmit();
        }}
      ></form>
    </FormProvider>
  );
}

But we are running into TS issue - "Type instantiation is excessively deep and possibly infinite" when trying to do this. I believe these kind of recursive field types are common and it should not cause issues. Maybe I am doing something wrong here? The issue is on this line

return (
    <FormProvider value={form}>
Your minimal, reproducible example

https://stackblitz.com/edit/tanstack-form-bsyh9m5c?file=src%2Findex.tsx

Steps to reproduce

See the reproduction here

https://stackblitz.com/edit/tanstack-form-bsyh9m5c?file=src%2Findex.tsx

Expected behavior

The type error should not be there

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

N/A

TanStack Form adapter

react-form

TanStack Form version

1.9.1

TypeScript version

No response

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the StackBlitz reproduction in src/index.tsx and reproduce the error at the FormProvider value={form} line. Inspect how the recursive FieldValues type interacts with ReactFormExtendedApi and the useForm result; done means the reproduction type-checks without the excessively-deep instantiation error.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.