Skip to content

Infer form data type from validator instead of default values #1583

@sebakerckhof

Description

@sebakerckhof

Currently the type of the form data seems to be inferred from the default values instead of the validator type. However, the validator is typically more complete/extended and since it's used for validation, it should always result in the correct type...

Currently, when I do something like (using zod in the example):

  const optionsForm = useAppForm({
    defaultValues: { foo: '' },
    validators: {
      onChange: z.object({ foo: z.string().optional() }),
    },
  });

This gives me a ts error, because it says the validation schema has foo as optional, while the default values have it filled in.

Another example with an enum type:

  const optionsForm = useAppForm({
    defaultValues: { foo: 'a' },
    validators: {
      onChange: z.object({ foo: z.enum(['a', 'b'])}),
    },
  });

This gives me a ts error because in the default values foo is infered as string, but the validator only accepts the more narrow 'a' | 'b' as type.

Since useAppForm takes a very large number of generics, manually specifying these isn't really a good alternative.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions