Skip to content

Commit a85cab8

Browse files
fix: simplify useForm return type (#1016)
* fix: simplify useForm return type * ci: apply automated fixes and generate docs * fix: restore validator type * ci: apply automated fixes and generate docs --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent c8e9887 commit a85cab8

File tree

6 files changed

+58
-10
lines changed

6 files changed

+58
-10
lines changed

Diff for: docs/framework/react/reference/functions/useform.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: useForm
66
# Function: useForm()
77

88
```ts
9-
function useForm<TFormData, TFormValidator>(opts?): FormApi<TFormData, TFormValidator> & ReactFormApi<TFormData, TFormValidator>
9+
function useForm<TFormData, TFormValidator>(opts?): ReactFormExtendedApi<TFormData, TFormValidator>
1010
```
1111

1212
A custom React Hook that returns an extended instance of the `FormApi` class.
@@ -25,8 +25,8 @@ This API encapsulates all the necessary functionalities related to the form. It
2525

2626
## Returns
2727

28-
`FormApi`\<`TFormData`, `TFormValidator`\> & [`ReactFormApi`](../interfaces/reactformapi.md)\<`TFormData`, `TFormValidator`\>
28+
[`ReactFormExtendedApi`](../type-aliases/reactformextendedapi.md)\<`TFormData`, `TFormValidator`\>
2929

3030
## Defined in
3131

32-
[useForm.tsx:57](https://github.com/TanStack/form/blob/main/packages/react-form/src/useForm.tsx#L57)
32+
[useForm.tsx:65](https://github.com/TanStack/form/blob/main/packages/react-form/src/useForm.tsx#L65)

Diff for: docs/framework/react/reference/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ title: "@tanstack/react-form"
1212
## Type Aliases
1313

1414
- [FieldComponent](type-aliases/fieldcomponent.md)
15+
- [ReactFormExtendedApi](type-aliases/reactformextendedapi.md)
1516
- [UseField](type-aliases/usefield.md)
1617

1718
## Functions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
id: ReactFormExtendedApi
3+
title: ReactFormExtendedApi
4+
---
5+
6+
# Type Alias: ReactFormExtendedApi\<TFormData, TFormValidator\>
7+
8+
```ts
9+
type ReactFormExtendedApi<TFormData, TFormValidator>: FormApi<TFormData, TFormValidator> & ReactFormApi<TFormData, TFormValidator>;
10+
```
11+
12+
An extended version of the `FormApi` class that includes React-specific functionalities from `ReactFormApi`
13+
14+
## Type Parameters
15+
16+
**TFormData**
17+
18+
**TFormValidator** *extends* `Validator`\<`TFormData`, `unknown`\> \| `undefined` = `undefined`
19+
20+
## Defined in
21+
22+
[useForm.tsx:55](https://github.com/TanStack/form/blob/main/packages/react-form/src/useForm.tsx#L55)

Diff for: packages/react-form/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export * from '@tanstack/form-core'
22

3-
export type { ReactFormApi } from './useForm'
3+
export type { ReactFormApi, ReactFormExtendedApi } from './useForm'
44
export { useForm } from './useForm'
55

66
export type { UseField, FieldComponent } from './useField'

Diff for: packages/react-form/src/useForm.tsx

+13-5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ export interface ReactFormApi<
4949
}) => NodeType
5050
}
5151

52+
/**
53+
* An extended version of the `FormApi` class that includes React-specific functionalities from `ReactFormApi`
54+
*/
55+
export type ReactFormExtendedApi<
56+
TFormData,
57+
TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
58+
> = FormApi<TFormData, TFormValidator> & ReactFormApi<TFormData, TFormValidator>
59+
5260
/**
5361
* A custom React Hook that returns an extended instance of the `FormApi` class.
5462
*
@@ -61,23 +69,23 @@ export function useForm<
6169
const [formApi] = useState(() => {
6270
const api = new FormApi<TFormData, TFormValidator>(opts)
6371

64-
const extendedApi: typeof api & ReactFormApi<TFormData, TFormValidator> =
72+
const extendedApi: ReactFormExtendedApi<TFormData, TFormValidator> =
6573
api as never
6674
extendedApi.Field = function APIField(props) {
67-
return (<Field {...props} form={api} />) as never
75+
return <Field {...props} form={api} />
6876
}
6977
// eslint-disable-next-line react-hooks/rules-of-hooks
7078
extendedApi.useField = (props) => useField({ ...props, form: api })
7179
extendedApi.useStore = (selector) => {
7280
// eslint-disable-next-line react-hooks/rules-of-hooks
73-
return useStore(api.store as any, selector as any) as any
81+
return useStore(api.store, selector)
7482
}
7583
extendedApi.Subscribe = (props) => {
7684
return functionalUpdate(
7785
props.children,
7886
// eslint-disable-next-line react-hooks/rules-of-hooks
79-
useStore(api.store as any, props.selector as any),
80-
) as any
87+
useStore(api.store, props.selector),
88+
)
8189
}
8290

8391
return extendedApi

Diff for: packages/react-form/tests/useForm.test-d.tsx

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import * as React from 'react'
21
import { assertType, it } from 'vitest'
32
import { useForm } from '../src/index'
3+
import type { ReactFormExtendedApi } from '../src/useForm'
44

55
it('should type onSubmit properly', () => {
66
function Comp() {
@@ -34,3 +34,20 @@ it('should type a validator properly', () => {
3434
})
3535
}
3636
})
37+
38+
it('should not have recursion problems and type register properly', () => {
39+
const register = <Data,>(f: ReactFormExtendedApi<Data>) => f
40+
41+
function Comp() {
42+
const form = useForm({
43+
defaultValues: {
44+
name: '',
45+
title: '',
46+
},
47+
})
48+
49+
const x = register(form)
50+
51+
return null
52+
}
53+
})

0 commit comments

Comments
 (0)