resolvers: yupResolver raise Types of parameters 'options' and 'options' are incompatible at react-hook-form 7.15.0

Hello.

Describe the bug

I updated react-hook-form 7.15.0, yupResolver raise following type error messages.

Type '<TFieldValues extends Record<string, any>, TContext>(values: UnpackNestedValue<TFieldValues>, context: TContext | undefined, options: ResolverOptions<TFieldValues>) => Promise<...>' is not assignable to type 'Resolver<FormType, object>'.
  Types of parameters 'options' and 'options' are incompatible.

Same code with react-hook-form 7.14.2 works fine(no type error).

versions

  • react: 17.0.2
  • react-hook-form: 7.15.0
  • @hookform/resolvers: 2.8.0
  • dayjs: 1.10.6
  • yup: 0.32.9
  • typescript: 4.3.5

To Reproduce

  1. Go to https://codesandbox.io/s/sad-wilson-rvpvv?file=/src/App.tsx
  2. move mouse cursor to line 22 and hover resolver
  3. Error message popup.

Codesandbox link (Required)

https://codesandbox.io/s/sad-wilson-rvpvv?file=/src/App.tsx:456-464

Expected behavior

No type error raised, or I want to know how to avoid type error (react-hook-form 7.14.2 works fine)

Screenshots

スクリーンショット 2021-09-07 22 26 31

Desktop (please complete the following information):

  • OS: macOS
  • Browser Chrome
  • Version 92.0.4515.159(Official Build) (x86_64)

Additional context

None

Thank you!

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 8
  • Comments: 24 (9 by maintainers)

Most upvoted comments

Hi, My current setup is:

@hookform/resolvers”: “^2.8.1”, “react-hook-form”: “7.15.4”, “yup”: “^0.32.9”, “@types/yup”: “^0.29.13”


They are the latest versions that are atm available and how I solved the above error is by using the yupResolver with the next generic type yupResolver<yup.AnyObjectSchema>. An example could be the following:


export type TModelRegisterFormInputs = {
    email: string;
    password: string;
    passwordConfirmation: string;
    role: Roles;
    callback?: any;
};

const schema = yup.object().shape({
    email: yup.string().required(),
    password: yup
        .string()
        .required()
        .min(4, 'Password is too short - should be 8 chars minimum.')
        .matches(/[a-zA-Z]/, 'Password can only contain Latin letters.'),
    passwordConfirmation: yup
        .string()
        .required()
        .oneOf([yup.ref('password'), null], 'Passwords must match'),
});

const {
        handleSubmit,
        formState: {errors},
        control,
    } = useForm<TModelRegisterFormInputs>({
        resolver: yupResolver<yup.AnyObjectSchema>(schema),
    });


You can also add the defaultValues and I would recommend you to do that.

Hope it helps you 😃

7.16.0 solve the original issue above. the other issue is related to 2.8.1 https://github.com/react-hook-form/resolvers/issues/245

hello there. I ran into the same type error with @hookform/resolvers: 2.8.1. My setup and error is below.

"react": "17.0.2",
"typescript": "^4.4.3",
"react-hook-form": "^7.15.4",
"@hookform/resolvers": "^2.8.1",
"yup": "^0.32.9"
12:54:52.034 | Type error: Type '(values: unknown, context: object, options: ResolverOptions<AssertsShape<Assign<ObjectShape, { email: RequiredStringSchema<string, Record<string, any>>; }>>>) => Promise<...>' is not assignable to type 'Resolver<{ email: string; }, object>'.
-- | --
12:54:52.034 | Types of parameters 'options' and 'options' are incompatible.
12:54:52.034 | Type 'ResolverOptions<{ email: string; }>' is not assignable to type 'ResolverOptions<AssertsShape<Assign<ObjectShape, { email: RequiredStringSchema<string, Record<string, any>>; }>>>'.
12:54:52.034 | Type '{ email: string; }' is not assignable to type 'AssertsShape<Assign<ObjectShape, { email: RequiredStringSchema<string, Record<string, any>>; }>>'.
12:54:52.034 | Property 'email' is incompatible with index signature.
12:54:52.034 | Type 'string' is not assignable to type 'never'.

@jorisre Thank you for fixing, but unfortunately still raise type error in my app…

https://codesandbox.io/s/hardcore-darwin-qusu6?file=/src/App.tsx スクリーンショット 2021-09-19 13 26 19

I Updated to @hookform 2.8.1 and react-hook-form 7.15.3

@asalahjawava Does your application works fine?

@heavenshell You can remove <FormType> from the useForm<FormType>. Types are inferred from the schema.