# use-form types
Package: @mantine/form
Import: import { formTypescript } from '@mantine/form';
Description: Form values types, generics, UseFormReturnType and TransformedValues

## Get values type

Form values type is inferred from `initialValues`. Use `typeof form.values` to reference it:

```tsx
import { useForm } from '@mantine/form';

function Demo() {
  const form = useForm({ initialValues: { name: '', age: 0 } });

  // Get the inferred form values type, will be `{ name: string; age: number }`
  type FormValues = typeof form.values;

  // Use the values type in handleSubmit function or anywhere else
  const handleSubmit = (values: FormValues) => console.log(values);
}
```

## Set values type

Pass a type to `useForm` when it cannot be inferred from `initialValues` or when you need a more specific type:

```tsx
import { useForm } from '@mantine/form';

interface FormValues {
  name: string; // regular field, same as inferred type
  role: 'user' | 'admin'; // union, more specific than inferred string type

  // values that may be undefined or null
  // cannot be correctly inferred in strict mode
  age: number | undefined;
  registeredAt: Date | null;

  // Arrays that are empty cannot be inferred correctly
  jobs: string[];
}

function Demo() {
  const form = useForm<FormValues>({
    initialValues: {
      name: '',
      role: 'user',
      age: undefined,
      registeredAt: null,
      jobs: [],
    },
  });
}
```

## TransformedValues type

`TransformedValues` is the type of [transformValues](https://alpha.mantine.dev/llms/form-submission.md#transformvalues) output.
Use it to type a submit handler declared outside of `form.onSubmit`:

```tsx
import { TransformedValues, useForm } from '@mantine/form';

function Demo() {
  const form = useForm({
    initialValues: {
      name: '',
      locationId: '2',
    },

    transformValues: (values) => ({
      ...values,
      locationId: Number(values.locationId),
    }),
  });

  type Transformed = TransformedValues<typeof form>;
  // -> { name: string, locationId: number }

  const handleSubmit = (values: TransformedValues<typeof form>) => {};

  return <form onSubmit={form.onSubmit(handleSubmit)} />;
}
```

## Set transformed values type

To set the transformed values type explicitly, pass a second type argument to `useForm`:

```tsx
import { useForm } from '@mantine/form';

interface FormValues {
  name: string;
  locationId: string;
}

interface TransformedValues {
  name: string;
  locationId: number;
}

function Demo() {
  const form = useForm<FormValues, TransformedValues>({
    initialValues: {
      name: '',
      locationId: '2',
    },

    transformValues: (values) => ({
      ...values,
      locationId: Number(values.locationId),
    }),
  });
}
```

## UseFormReturnType

Use `UseFormReturnType` to type `form` passed as a prop.
To share the form with many nested components, use [form context](https://alpha.mantine.dev/llms/form-create-form-context.md) instead:

```tsx
import { TextInput } from '@mantine/core';
import { useForm, UseFormReturnType } from '@mantine/form';

interface FormValues {
  name: string;
  occupation: string;
}

function NameInput({
  form,
}: {
  form: UseFormReturnType<FormValues>;
}) {
  return (
    <TextInput
      {...form.getInputProps('name')}
    />
  );
}

function OccupationInput({
  form,
}: {
  form: UseFormReturnType<FormValues>;
}) {
  return (
    <TextInput
      {...form.getInputProps('occupation')}
    />
  );
}

function Demo() {
  const form = useForm<FormValues>({
    initialValues: { name: '', occupation: '' },
  });
  return (
    <>
      <NameInput form={form} />
      <OccupationInput form={form} />
    </>
  );
}
```

## FormPath type

`FormPath<Values, Value>` is a union of paths of form values with the given value type.
It is used to type components that render [groups of fields](https://alpha.mantine.dev/llms/form-reusable-fields.md#groups-of-fields):

```tsx
import { FormPath } from '@mantine/form';

interface FormValues {
  name: string;
  shipping: { street: string; city: string };
  billing: { street: string; city: string };
}

type AddressPath = FormPath<FormValues, { street: string; city: string }>;
// -> 'shipping' | 'billing'
```

## FormErrors type

`form.errors` type is `Record<string, React.ReactNode>`, it is exported as `FormErrors`,
see [Form errors](https://alpha.mantine.dev/llms/form-errors.md#formerrors-type).

## Validation return types

`form.validate`, `form.validateField` and `form.isValid` return results directly with sync rules.
With any async rule, they return a `Promise`.
With [schemaResolver](https://alpha.mantine.dev/llms/form-schema-validation.md#sync-and-async-schemas), pass `{ sync: true }` to get
sync return types.
