react-hook-form / resolvers

📋 Validation resolvers: Yup, Zod, Superstruct, Joi, Vest, Class Validator, io-ts, Nope, computed-types, typanion, Ajv, TypeBox, ArkType, Valibot, effect-ts and VineJS
https://react-hook-form.com/
MIT License
1.72k stars 157 forks source link

TypeBox resolver does not work with valid TypeBox schema #709

Closed divmgl closed 1 month ago

divmgl commented 1 month ago

Describe the bug I'm getting this error:

Argument of type 'TObject<{ name: TString; score: TString; externalId: any; }>' is not assignable to parameter of type 'TObject<TProperties> | TypeCheck<TObject<TProperties>>'.ts(2345)
Argument of type 'TObject<{ name: TString; score: TString; externalId: any; }>' is not assignable to parameter of type 'TObject<TProperties> | TypeCheck<TObject<TProperties>>'.
  Type 'TObject<{ name: TString; score: TString; externalId: any; }>' is missing the following properties from type 'TypeCheck<TObject<TProperties>>': schema, references, checkFunc, code, and 3 more.ts(2345)

Here is the TypeBox schema:

export const UPDATE_APPLICATION_INPUT_SCHEMA = Type.Object({
  name: Type.String({ minLength: 1 }),
  score: Type.String(),
  externalId: Type.String().Nullable(),
})
  const form = useForm<UpdateApplicationInput>({
    resolver: typeboxResolver(UPDATE_APPLICATION_INPUT_SCHEMA),
    defaultValues: {
      name: application.name,
      externalId: application.externalId ?? null,
    },
  })

Why is this happening?

Expected behavior No error occurs.

Additional context TypeBox version: 0.32.35 @hookform/resolvers: 3.9.0

divmgl commented 1 month ago

Also does not work with the new TypeCompiler.Compile. Could it be because of our TypeScript version? We're on 5.4.5.

divmgl commented 1 month ago

@huynhducduy apologies for pinging you here. I see that you worked on this for 3.8.0. https://github.com/react-hook-form/resolvers/pull/674

Did you run into this during development? I'm thinking about switching my codebase over to TypeBox to overcome the Zod performance issues, but I'm running into this problem with @hookform/resolvers which is blocking the switch.

huynhducduy commented 1 month ago

@divmgl can you first try to use it with @hookform/resolvers 3.7.0? and maybe with a newer typescript version too. Im using typescript 5.5.3 and it works just fine.

divmgl commented 1 month ago

Hm, I wonder if this is a TypeBox problem.

Argument of type 'TObject<{ name: TString; groupId: TString; }>' is not assignable to parameter of type 'TObject<TProperties>'.
  Property '[Kind]' is missing in type 'TObject<{ name: TString; groupId: TString; }>' but required in type 'TObject<TProperties>'.ts(2345)
typebox.d.ts(240, 5): '[Kind]' is declared here.

This is happening in 3.7.0. Really strange.

huynhducduy commented 1 month ago

I did not face any similar problems. Can you reproduce the error in a fresh source code?

divmgl commented 1 month ago

I cleaned the repo and removed incremental build info files and things are working as expected now. Cheers.