Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

useForm.test-d.tsx

53 lines1.1 KB

1import { assertType, it } from 'vitest'
2import { useForm } from '../src/index'
3import type { ReactFormExtendedApi } from '../src/useForm'
4​
5it('should type onSubmit properly', () => {
6 function Comp() {
7 const form = useForm({
8 defaultValues: {
9 firstName: 'test',
10 age: 84,
11 // as const is required here
12 } as const,
13 onSubmit({ value }) {
14 assertType<84>(value.age)
15 },
16 })
17 }
18})
19​
20it('should type a validator properly', () => {
21 function Comp() {
22 const form = useForm({
23 defaultValues: {
24 firstName: 'test',
25 age: 84,
26 // as const is required here
27 } as const,
28 validators: {
29 onChange({ value }) {
30 assertType<84>(value.age)
31 return undefined
32 },
33 },
34 })
35 }
36})
37​
38it('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})