form icon indicating copy to clipboard operation
form copied to clipboard

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

Open rishitells opened this issue 7 months ago • 1 comments

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

rishitells avatar May 07 '25 03:05 rishitells

it's not precise, but perhaps you could try removing ReactNode type or narrowing the type inference.

i actually encountered the same issue recently. in my case, it happened when I tried form composition using withForm. the root cause turned out to be that one of the properties declared in defaultValues within the formOptions used in useAppForm had a ReactNode type.

all other properties were of type string!

after removing that, the error disappeared. let me share a portion of my code with you.

type Information = {
    id: string;
    // ... ...
    target: ReactNode,  // this line was the cause of the issue.
}

export const defaultInformation: Information= {...}


function Playground() {
  const form = useAppForm({
    ...defaultInformation,
    onSubmit: ({ value }) => { ... },
  });

  // ...
}

taylous avatar Jul 17 '25 14:07 taylous