1
+ import { Resolver , SubmitHandler , useForm } from 'react-hook-form' ;
1
2
import { computedTypesResolver } from '..' ;
2
3
import { fields , invalidData , schema , validData } from './__fixtures__/data' ;
4
+ import Schema , { number } from 'computed-types' ;
3
5
4
6
const shouldUseNativeValidation = false ;
5
7
@@ -28,7 +30,6 @@ describe('computedTypesResolver', () => {
28
30
} ) ;
29
31
30
32
const promise = computedTypesResolver ( schemaWithCustomError ) (
31
- // @ts -expect-error expect to throw
32
33
validData ,
33
34
undefined ,
34
35
{
@@ -39,4 +40,57 @@ describe('computedTypesResolver', () => {
39
40
40
41
await expect ( promise ) . rejects . toThrow ( 'custom error' ) ;
41
42
} ) ;
43
+
44
+ /**
45
+ * Type inference tests
46
+ */
47
+ it ( 'should correctly infer the output type from a computedTypes schema' , ( ) => {
48
+ const resolver = computedTypesResolver ( Schema ( { id : number } ) ) ;
49
+
50
+ expectTypeOf ( resolver ) . toEqualTypeOf <
51
+ Resolver < { id : number } , unknown , { id : number } >
52
+ > ( ) ;
53
+ } ) ;
54
+
55
+ it ( 'should correctly infer the output type from a computedTypes schema using a transform' , ( ) => {
56
+ const resolver = computedTypesResolver (
57
+ Schema ( { id : number . transform ( ( val ) => String ( val ) ) } ) ,
58
+ ) ;
59
+
60
+ expectTypeOf ( resolver ) . toEqualTypeOf <
61
+ Resolver < { id : number } , unknown , { id : string } >
62
+ > ( ) ;
63
+ } ) ;
64
+
65
+ it ( 'should correctly infer the output type from a computedTypes schema for the handleSubmit function in useForm' , ( ) => {
66
+ const schema = Schema ( { id : number } ) ;
67
+
68
+ const form = useForm ( {
69
+ resolver : computedTypesResolver ( schema ) ,
70
+ } ) ;
71
+
72
+ expectTypeOf ( form . watch ( 'id' ) ) . toEqualTypeOf < number > ( ) ;
73
+
74
+ expectTypeOf ( form . handleSubmit ) . parameter ( 0 ) . toEqualTypeOf <
75
+ SubmitHandler < {
76
+ id : number ;
77
+ } >
78
+ > ( ) ;
79
+ } ) ;
80
+
81
+ it ( 'should correctly infer the output type from a computedTypes schema with a transform for the handleSubmit function in useForm' , ( ) => {
82
+ const schema = Schema ( { id : number . transform ( ( val ) => String ( val ) ) } ) ;
83
+
84
+ const form = useForm ( {
85
+ resolver : computedTypesResolver ( schema ) ,
86
+ } ) ;
87
+
88
+ expectTypeOf ( form . watch ( 'id' ) ) . toEqualTypeOf < number > ( ) ;
89
+
90
+ expectTypeOf ( form . handleSubmit ) . parameter ( 0 ) . toEqualTypeOf <
91
+ SubmitHandler < {
92
+ id : string ;
93
+ } >
94
+ > ( ) ;
95
+ } ) ;
42
96
} ) ;
0 commit comments