Type error when passing field from `useForm(formOptions(...))` to `AnyFieldApi` prop #1794
Describe the bug#
When I try to pass field object (FieldApi) to prop of type AnyFieldApi I get a type error. It only happens when the form object from useForm was created using the formOptions function.
TS2322: Type
FieldApi<{ firstName: string; }, "firstName", string, FieldValidateOrFn<{ firstName: string; }, "firstName", string> | undefined, ({ value }: { value: string; fieldApi: FieldApi<{ ...; }, ... 21 more ..., any>; }) => "A first name is required" | ... 1 more ... | undefined, ... 17 more ..., never>
is not assignable to type AnyFieldApi
The types of form.options.listeners are incompatible between these types.
Type 'FormListeners<{ firstName: string; }, FormValidateOrFn<{ firstName: string; }> | undefined, FormValidateOrFn<{ firstName: string; }> | undefined, FormAsyncValidateOrFn<...> | undefined, ... 7 more ..., never> | undefined' is not assignable to type 'FormListeners<any, any, any, any, any, any, any, any, any, any, any, any> | undefined'.
Type 'FormListeners<{ firstName: string; }, FormValidateOrFn<{ firstName: string; }> | undefined, FormValidateOrFn<{ firstName: string; }> | undefined, FormAsyncValidateOrFn<...> | undefined, ... 7 more ..., never>' is not assignable to type 'FormListeners<any, any, any, any, any, any, any, any, any, any, any, any>'.
Types of property onChange are incompatible.
Type '((props: { formApi: FormApi<{ firstName: string; }, FormValidateOrFn<{ firstName: string; }> | undefined, FormValidateOrFn<{ firstName: string; }> | undefined, ... 8 more ..., never>; fieldApi: AnyFieldApi; }) => void) | undefined' is not assignable to type '((props: { formApi: FormApi<any, any, any, any, any, any, any, any, any, any, any, any>; fieldApi: AnyFieldApi; }) => void) | undefined'.
Type '(props: { formApi: FormApi<{ firstName: string; }, FormValidateOrFn<{ firstName: string; }> | undefined, FormValidateOrFn<{ firstName: string; }> | undefined, ... 8 more ..., never>; fieldApi: AnyFieldApi; }) => void' is not assignable to type '(props: { formApi: FormApi<any, any, any, any, any, any, any, any, any, any, any, any>; fieldApi: AnyFieldApi; }) => void'.
Types of parameters props and props are incompatible.
Type '{ formApi: FormApi<any, any, any, any, any, any, any, any, any, any, any, any>; fieldApi: AnyFieldApi; }' is not assignable to type '{ formApi: FormApi<{ firstName: string; }, FormValidateOrFn<{ firstName: string; }> | undefined, FormValidateOrFn<{ firstName: string; }> | undefined, ... 8 more ..., never>; fieldApi: AnyFieldApi; }'.
The types of formApi.options.listeners are incompatible between these types.
Type 'FormListeners<any, any, any, any, any, any, any, any, any, any, any, any> | undefined' is not assignable to type 'FormListeners<{ firstName: string; }, FormValidateOrFn<{ firstName: string; }> | undefined, FormValidateOrFn<{ firstName: string; }> | undefined, FormAsyncValidateOrFn<...> | undefined, ... 7 more ..., never> | undefined'.
Type 'FormListeners<any, any, any, any, any, any, any, any, any, any, any, any>' is not assignable to type 'FormListeners<{ firstName: string; }, FormValidateOrFn<{ firstName: string; }> | undefined, FormValidateOrFn<{ firstName: string; }> | undefined, FormAsyncValidateOrFn<...> | undefined, ... 7 more ..., never>'.
Type any is not assignable to type never
Your minimal, reproducible example#
https://stackblitz.com/edit/vitejs-vite-qffrfz9w?file=src%2FApp.tsx
Steps to reproduce#
- Open stackblitz repro.
- See the type error you get when you try to pass
fieldobject tofieldprop ofFieldInfocomponent. - Delete
onSubmithandler fromformOptions. - The type error is gone.
Also, if you take the stuff you pass into formOptions and directly pass it to useForm, (skipping formOptions completely) there is no type error. But I need to reuse the options in other useForm calls.
The repro code is the example from https://tanstack.com/form/latest, when you scroll all the way down. I just moved the options to formOptions instead of passing them directly to useForm.
Expected behavior#
The type error should not happen when onSubmit handler is present in formOptions.
How often does this bug happen?#
Every time
Screenshots or Videos#
Platform#
rocky linux, chrome 139
TanStack Form adapter#
react-form and svelte adapter. I didn't check other frameworks.
TanStack Form version#
^1.23.5
TypeScript version#
~5.9.3
Additional context#
Bladesheng(GitHub profile, opens in a new tab) changed the title
LeCarbonator(GitHub profile, opens in a new tab) added labelstype: bug
Checking out possible solutions with #1964
If it helps anyone, using @Bladesheng's example I managed to calm the type error by splitting out the options passed to
formOptions()like this:import { type AnyFieldApi, formOptions, type FormValidateFn, useForm, } from '@tanstack/react-form'; const defaultValues = { firstName: '' }; const onSubmit: FormValidateFn<typeof defaultValues> = async ({ value, }) => { console.log(value); }; const formOpts = formOptions({ defaultValues, onSubmit, }); export default function App() { const form = useForm({ ...formOpts }); ...Where the
defaultValues,onSubmit, and call toformOptions()could be in their own file. I am unsure if there's any downside here, please let me know if you can foresee any issues with this.I hope this meets the "reuse" requirement you mentioned here @Bladesheng:
Also, if you take the stuff you pass into formOptions and directly pass it to useForm, (skipping formOptions completely) there is no type error. But I need to reuse the options in other useForm calls.
I'll look into it! @ThrowingSpoon
LeCarbonator(GitHub profile, opens in a new tab) added labelsv1
LeCarbonator(GitHub profile, opens in a new tab) added labelsv2: needs investigation
Sign in to join this conversation.
I believe the same issue arises with
listenersif they were added toformOptions. It ends up being because of howformOptionsis typed.Thanks for the report!