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

createField.test-d.tsx

74 lines1.5 KB

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