Type error on generic form type with a field of type File #2145
Describe the bug#
Hello, I am trying to create a context for a generic AppForm. If the form has a field which may be a JS File object, the following type error is shown:
error TS2322: Type 'AppFieldExtendedReactFormApi<{ firstName: string; formMode: FormMode; formFile: File | null; }, FormValidateOrFn<{ firstName: string; formMode: FormMode; formFile: File | null; }> | undefined, ... 11 more ..., {}>' is not assignable to type 'AppFieldExtendedReactFormApi<any, any, any, any, any, any, any, any, any, any, any, any, { readonly TextInput: ({ ...props }: DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>) => Element; }, {}>'.
Types of property 'pushFieldValue' are incompatible.
Type '<TField extends never>(field: TField, value: (TField extends "formMode" | "firstName" | "formFile" | "formFile.text" | `formFile.text.${string}` | "formFile.name" | "formFile.size" | "formFile.type" | ... 9 more ... | `formFile.stream.${string}` ? DeepRecord<...>[TField] : never) extends any[] ? (any[] & (TField ext...' is not assignable to type '<TField extends never>(field: TField, value: any, options?: UpdateMetaOptions | undefined) => void'.
Types of parameters 'value' and 'value' are incompatible.
Type 'any' is not assignable to type 'never'.
Please see the stackblitz example for the full set up
Your minimal, reproducible example#
https://stackblitz.com/edit/tanstack-form-zz8oewgu
Steps to reproduce#
- Create a form with a field that may be a File
- Set up a context with a generic form (this is not straightforward, see discussion #2144 )
- Passing the form instance to the form will show the type error
Expected behavior#
No type error should be raised
How often does this bug happen?#
None
Screenshots or Videos#
No response
Platform#
- Windows
- Chrome
TanStack Form adapter#
None
TanStack Form version#
1.29.1
TypeScript version#
6.0.2
Additional context#
No response
crutchcorn(GitHub profile, opens in a new tab) closed this
I tried using
withFormwith no success 😅 Thanks to AI, I found this working and type safe solution:import { createFormHook, createFormHookContexts, type AppFieldExtendedReactFormApi } from "@tanstack/react-form"; import { createContext, use, type ReactNode } from "react"; /* ... fields and components ... */ const { fieldContext, formContext, useFieldContext } = createFormHookContexts(); export { useFieldContext }; const fieldComponents = { /* ... field components ... */ }; const formComponents = { /* ... form components .. */ }; export const { useAppForm, withForm } = createFormHook({ fieldComponents, fieldContext, formComponents, formContext, }); type AppFormApi = AppFieldExtendedReactFormApi< any, any, any, any, any, any, any, any, any, any, any, any, typeof fieldComponents, typeof formComponents >; export interface BaseFormContextProps { form: AppFormApi; isLoading?: boolean; /* ... other custom props */ } const BaseFormContext = createContext<BaseFormContextProps | undefined>(undefined); export interface FormContextProps BaseFormContextProps { form: AppFormApi; /* custom props for the wrapper, not stored in the context */ className?: string; children?: ReactNode; } export function FormContextProvider({ form, isLoading = false, className, children, }: FormContextProps) { return ( <BaseFormContext value={{ form, formMode, isLoading, validationSchema }}> <form.AppForm> <div className={cn("relative flex max-w-full flex-col gap-3", className)}> {children} {isLoading && <p>Loading overlay ...</p>} </div> </form.AppForm> </BaseFormContext> ); } export const BaseForm = FormContextProvider; export function useFormContext() { const baseContext = use(BaseFormContext); if (!baseContext) { throw new Error("useFormContext must be used within a BaseForm component"); } return baseContext; }Is this a good approach?
Sign in to join this conversation.
Using generics/internal types directly like this is not supported. Use
withFormandformOptionsinstead:https://tanstack.com/form/latest/docs/framework/react/guides/form-composition#breaking-big-forms-into-smaller-pieces