Skip to content

Commit 587ec6c

Browse files
committed
fix(typebox): improve type interface
1 parent a45a85e commit 587ec6c

File tree

1 file changed

+7
-29
lines changed

1 file changed

+7
-29
lines changed

typebox/src/typebox.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
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';
5-
import {
6-
FieldError,
7-
FieldErrors,
8-
FieldValues,
9-
Resolver,
10-
appendErrors,
11-
} from 'react-hook-form';
5+
import { FieldError, Resolver, appendErrors } from 'react-hook-form';
126

137
function parseErrorSchema(
148
_errors: ValueError[],
@@ -45,22 +39,6 @@ function parseErrorSchema(
4539
return errors;
4640
}
4741

48-
export function typeboxResolver<Input extends FieldValues, Context>(
49-
typecheck: TypeCheck<ReturnType<typeof Type.Object<Input>>>,
50-
): Resolver<
51-
Static<ReturnType<typeof typecheck.Schema>>,
52-
Context,
53-
StaticDecode<ReturnType<typeof typecheck.Schema>>
54-
>;
55-
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>;
63-
6442
/**
6543
* Creates a resolver for react-hook-form using Typebox schema validation
6644
* @param {Schema | TypeCheck<Schema>} schema - The Typebox schema to validate against
@@ -77,10 +55,10 @@ export function typeboxResolver<Input extends FieldValues, Context, Output>(
7755
* resolver: typeboxResolver(schema)
7856
* });
7957
*/
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) => {
58+
export function typeboxResolver<Schema extends TObject, Context>(
59+
schema: Schema | TypeCheck<Schema>,
60+
): Resolver<Static<Schema>, Context, StaticDecode<Schema>> {
61+
return async (values: Static<Schema>, _, options) => {
8462
const errors = Array.from(
8563
schema instanceof TypeCheck
8664
? schema.Errors(values)
@@ -91,7 +69,7 @@ export function typeboxResolver<Input extends FieldValues, Context, Output>(
9169

9270
if (!errors.length) {
9371
return {
94-
errors: {} as FieldErrors,
72+
errors: {},
9573
values,
9674
};
9775
}

0 commit comments

Comments
 (0)