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

Typescript Error on optional Fields with Yup #599

Closed friedensstifter closed 1 year ago

friedensstifter commented 1 year ago

Description: If a schema is defined with yup and a field is not marked as required, resolver show an error.

const schema = object({
  title: string(),
  news: string().required(),
});

type FormSchema = InferType<typeof schema>;

export default function NewsAdd() {
  const methods = useForm<FormSchema>({
    resolver: yupResolver(schema),
    mode: "onChange",
    reValidateMode: "onChange",
  });

TS-Error:

Type 'Resolver<{ title: string | undefined; news: string; }>' is not assignable to type 'Resolver<{ title?: string | undefined; news: string; }, any>'. Types of parameters 'values' and 'values' are incompatible. Type '{ title?: string | undefined; news: string; }' is not assignable to type '{ title: string | undefined; news: string; }'.ts(2322)

Can't post codesandox-link. Codesandboy stays at v3.0.0. This works perfect. Only v3.1.1 got this problem.

Expected behavior I need to make fields optional. Set everything to required is a problem.

Cigan12 commented 1 year ago

Same here Error: Type 'Resolver<{ name: string | undefined; description: string | undefined; availableOnMobile: boolean | undefined; assign: boolean | undefined; recommend: boolean | undefined; duration: string | undefined; }>' is not assignable to type 'Resolver<{ name?: string | undefined; description?: string | undefined; availableOnMobile?: boolean | undefined; assign?: boolean | undefined; recommend?: boolean | undefined; duration?: string | undefined; }, any>'. Types of parameters 'values' and 'values' are incompatible. Type '{ name?: string | undefined; description?: string | undefined; availableOnMobile?: boolean | undefined; assign?: boolean | undefined; recommend?: boolean | undefined; duration?: string | undefined; }' is not assignable to type '{ name: string | undefined; description: string | undefined; availableOnMobile: boolean | undefined; assign: boolean | undefined; recommend: boolean | undefined; duration: string | undefined; }' Versions: yup: 1.2.0 @hookform/resolvers: 3.1.1 react-hook-form: 7.45.2

Reproduction: https://codesandbox.io/s/react-typescript-forked-vsszfm?file=/src/App.tsx

tcolinpa commented 1 year ago

Same thing happens on zodResolver, created an issue #612

Naddiseo commented 1 year ago

It does look like it was introduced by the change in 3.1.1, the reproduction in the original post can be made to work by following the instructions in the 3.1.1 release notes:

const schema = object({
  title: string(),
  news: string().required(),
});

type FormSchema = InferType<typeof schema>;

export default function NewsAdd() {
- const methods = useForm<FormSchema>({
+ const methods = useForm({
    resolver: yupResolver(schema),
    mode: "onChange",
    reValidateMode: "onChange",
  });
jorisre commented 1 year ago

@Naddiseo is right. I'm closing.