Skip to content

Commit 92b67c7

Browse files
author
Harry Whorlow
committed
feat(react): UseFormReturnType utility type
1 parent bb46a92 commit 92b67c7

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

docs/framework/react/guides/reusable-fields.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ import { useForm, Validator, InferValidFormKeys } from '@tanstack/react-form';
6262

6363
export default function GenericTextField<
6464
TForm,
65+
TFormValidator
6566
TName extends InferValidFormKeys<TForm, string>,
66-
TFormValidator extends Validator<TForm, unknown> | undefined,
6767
>({ name, form }: {
6868
name: TName;
69-
form: ReturnType<typeof useForm<TForm, TFormValidator>
69+
form: UseFormReturnType<TForm, TFormValidator>
7070
> }): JSX.Element {
7171
return (
7272
<form.Field name={name}>

packages/react-form/src/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
Validator,
66
} from '@tanstack/form-core'
77
import type { FunctionComponent } from 'react'
8+
import { useForm } from './useForm'
89

910
/**
1011
* The field options.
@@ -28,3 +29,12 @@ export type UseFieldOptions<
2829
> & {
2930
mode?: 'value' | 'array'
3031
}
32+
33+
/**
34+
* The return type use useForm with pre-populated generics
35+
*/
36+
export type UseFormReturnType<TForm, TFormValidator> = TFormValidator extends
37+
| Validator<TForm, unknown>
38+
| undefined
39+
? ReturnType<typeof useForm<TForm, TFormValidator>>
40+
: never

0 commit comments

Comments
 (0)