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

useField.test-d.tsx

67 lines1.4 KB

1import { assertType } from 'vitest'
2import * as React from 'react'
3import { useForm } from '../useForm'
4​
5it('should type state.value properly', () => {
6 function Comp() {
7 const form = useForm({
8 defaultValues: {
9 firstName: 'test',
10 age: 84,
11 },
12 } as const)
13​
14 return (
15 <form.Provider>
16 <form.Field
17 name="firstName"
18 children={(field) => {
19 assertType<'test'>(field.state.value)
20 }}
21 />
22 <form.Field
23 name="age"
24 children={(field) => {
25 assertType<84>(field.state.value)
26 }}
27 />
28 </form.Provider>
29 )
30 }
31})
32​
33it('should type onChange properly', () => {
34 function Comp() {
35 const form = useForm({
36 defaultValues: {
37 firstName: 'test',
38 age: 84,
39 },
40 } as const)
41​
42 return (
43 <form.Provider>
44 <form.Field
45 name="firstName"
46 validators={{
47 onChange: ({ value }) => {
48 assertType<'test'>(value)
49 return null
50 },
51 }}
52 children={() => null}
53 />
54 <form.Field
55 name="age"
56 validators={{
57 onChange: ({ value }) => {
58 assertType<84>(value)
59 return null
60 },
61 }}
62 children={() => null}
63 />
64 </form.Provider>
65 )
66 }
67})