Skip to content

Commit 50d29eb

Browse files
committed
fix(typebox): improve type interface
1 parent a45a85e commit 50d29eb

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

typebox/src/typebox.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
2-
import { Static, StaticDecode, Type } from '@sinclair/typebox';
2+
import { Static, StaticDecode, TObject } from '@sinclair/typebox';
33
import { TypeCheck } from '@sinclair/typebox/compiler';
44
import { Value, type ValueError } from '@sinclair/typebox/value';
55
import {
66
FieldError,
7-
FieldErrors,
87
FieldValues,
98
Resolver,
109
appendErrors,
@@ -45,21 +44,19 @@ function parseErrorSchema(
4544
return errors;
4645
}
4746

48-
export function typeboxResolver<Input extends FieldValues, Context>(
49-
typecheck: TypeCheck<ReturnType<typeof Type.Object<Input>>>,
50-
): Resolver<
51-
Static<ReturnType<typeof typecheck.Schema>>,
47+
export function typeboxResolver<
48+
Input extends FieldValues,
5249
Context,
53-
StaticDecode<ReturnType<typeof typecheck.Schema>>
54-
>;
50+
Schema extends TObject<Input> = TObject<Input>,
51+
>(
52+
typecheck: TypeCheck<Schema>,
53+
): Resolver<Static<Schema>, Context, StaticDecode<Schema>>;
5554

56-
export function typeboxResolver<Input extends FieldValues, Context>(
57-
schema: ReturnType<typeof Type.Object<Input>>,
58-
): Resolver<Static<typeof schema>, Context, StaticDecode<typeof schema>>;
59-
60-
export function typeboxResolver<Input extends FieldValues, Context, Output>(
61-
schema: ReturnType<typeof Type.Object<Input>>,
62-
): Resolver<Static<typeof schema>, Context, Output>;
55+
export function typeboxResolver<
56+
Input extends FieldValues,
57+
Context,
58+
Schema extends TObject<Input> = TObject<Input>,
59+
>(schema: Schema): Resolver<Static<Schema>, Context, StaticDecode<Schema>>;
6360

6461
/**
6562
* Creates a resolver for react-hook-form using Typebox schema validation
@@ -77,10 +74,14 @@ export function typeboxResolver<Input extends FieldValues, Context, Output>(
7774
* resolver: typeboxResolver(schema)
7875
* });
7976
*/
80-
export function typeboxResolver<Input extends FieldValues, Context, Output>(
81-
schema: ReturnType<typeof Type.Object<Input>>,
82-
): Resolver<Static<typeof schema>, Context, Output | Static<typeof schema>> {
83-
return async (values: Static<typeof schema>, _, options) => {
77+
export function typeboxResolver<
78+
Input extends FieldValues,
79+
Context,
80+
Schema extends TObject<Input>,
81+
>(
82+
schema: Schema | TypeCheck<Schema>,
83+
): Resolver<Static<Schema>, Context, StaticDecode<Schema>> {
84+
return async (values: Static<Schema>, _, options) => {
8485
const errors = Array.from(
8586
schema instanceof TypeCheck
8687
? schema.Errors(values)
@@ -91,7 +92,7 @@ export function typeboxResolver<Input extends FieldValues, Context, Output>(
9192

9293
if (!errors.length) {
9394
return {
94-
errors: {} as FieldErrors,
95+
errors: {},
9596
values,
9697
};
9798
}

0 commit comments

Comments
 (0)