TanStack / form

🤖 Powerful and type-safe form state management for the web. TS/JS, React Form, Solid Form, Lit Form and Vue Form.

Home Page:https://tanstack.com/form

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Calling `createFormFactory<FormData>` and using a validator adapter throw TS errors

timfee opened this issue · comments

Describe the bug

I had to pass typeof zodValidator as a second type param for validatorAdapter to not throw a TS warning.

Your minimal, reproducible example

export type FormSchema = { id: string } 

export const formFactory = createFormFactory<FormSchema>({
  validatorAdapter: zodValidator,
// ...

Expected behavior

I didn't expect the following error:

Type 'ZodString' is not assignable to type 'FieldValidateFn<Partial<{ slug: string; url: string; is_locked: boolean; is_public: boolean; id?: string | undefined; }>, "slug", undefined, undefined, string | undefined>'.

Type 'ZodString' provides no match for the signature '(props: { value: string | undefined; fieldApi: FieldApi<Partial<{ slug: string; url: string; is_locked: boolean; is_public: boolean; id?: string | undefined; }>, "slug", undefined, undefined, string | undefined>; }): ValidationError'.

It wasn't until I added the second type param: createFormFactory<FormSchema, typeof zodValidator> that it went away.

(I'm not sure if that's technically correct or the desired DX :))

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

react + ts

TanStack Form adapter

react-form

TanStack Form version

0.19.0

TypeScript version

5.4.4

Additional context

No response

This is almost a dupe of #610, but heads up that you should not provide the default type and expect inferencing (say, of the validator) to still run. This is a limitation of TS, not our library.