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
it's not precise, but perhaps you could try removing
ReactNodetype 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 indefaultValueswithin theformOptionsused inuseAppFormhad aReactNodetype.after removing that, the error disappeared. let me share a portion of my code with you.