resolvers: Type problems after update resolver to 1.3.0

After update @hookform/resolvers to the version 1.3.0, this code above no longer works.

The TS compiler points out the error:

Type 'Resolver<AssertsShape<Assign<Record<string, AnySchema<any, any, any> | Reference<unknown> | Lazy<any, any>>, { title: RequiredStringSchema<string, Record<string, any>>; buttonTitle: RequiredStringSchema<...>; exibitionMode: RequiredStringSchema<...>; link: RequiredStringSchema<...>; duration: NumberSchema<...>; }>>,...' is not assignable to type 'Resolver<FormData, object>'.
  Types of parameters 'values' and 'values' are incompatible.
    Type 'FormData' is not assignable to type '{ [x: string]: never; title: string; buttonTitle: string; exibitionMode: string; link: string; duration: number; }'.
      Index signature is missing in type 'FormData'.ts(2322)
form.d.ts(39, 5): The expected type comes from property 'resolver' which is declared here on type 'Partial<{ mode: "onBlur" | "onChange" | "onSubmit" | "onTouched"

If I comment the lines of exibitionMode and colorBackground the error disappears.

This is the code that I’m working with

import { useForm } from 'react-hook-form'
import { yupResolver } from '@hookform/resolvers/yup'
import { object, string, number } from 'yup'

enum Color {
  DARK = 'dark',
  LIGHT = 'light',
  PURPLE = 'purple',
  ORANGE = 'orange',
  RED = 'red',
  GREEN = 'green'
}

enum ExibitionMode {
  INFINITY = 'infinity',
  LIMITED = 'limited'
}

interface FormData {
  title: string,
  buttonTitle: string,
  exibitionMode: ExibitionMode,
  link: string,
  duration: number,
  colorBackground: Color
}

const TITLE_MAX_LENGTH = 96
const BUTTON_TITLE_MAX_LENGTH = 28

const schema = object().shape({
  title: string().max(TITLE_MAX_LENGTH).required(),
  buttonTitle: string().max(BUTTON_TITLE_MAX_LENGTH).required(),
  exibitionMode: string().required(),
  link: string().required().url(),
  duration: number().when('exibitionMode', (exibitionMode, schema) => {
    return exibitionMode === ExibitionMode.LIMITED ? schema.min(1).required() : schema
  })
})

const Test = () => {
  const { register, handleSubmit } = useForm<FormData>({
    resolver: yupResolver(schema),
    mode: 'onBlur'
  })
}

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 7
  • Comments: 32 (12 by maintainers)

Commits related to this issue

Most upvoted comments

I am getting this error in 2.8.1 again. 2.8.0 is fine.

@mrlubos My solution to this problem was to the code below in my jest.config file

 "transformIgnorePatterns": [
    '/node_modules/(?!@hookform/*).+\\.[t|j]sx?$'
  ],

I don’t know if is right, but it worked

Fixed in 1.3.2

Any feedback appreciated 😃

@jorisre the implication is that you made a breaking change, albeit unintentional, in a minor version, and that you’re not planning to fix it in the v1 line. Can you confirm?

(semver would dictate that you revert back to v1.2.x and publish it as a v1.3 latest, at the very least)

What a coincidence, I just encountered this exact same issue on a new project, 30 minutes ago. Thank you for your quick response and fix, it works now! 💪

i can’t quite reproduce it in codeandsandbox, but i have included the schema definitions in a small snippet, and below is one of the errors i got: Type 'Resolver<AssertsShape<Assign<Record<string, AnySchema<any, any, any> | Reference<unknown> | Lazy<any, any>>, { salutation: RequiredStringSchema<string, Record<string, any>>; ... 20 more ...; concentrator: BooleanSchema<...>; }>>, object>' is not assignable to type 'Resolver<IFormInputs, object>'. Types of parameters 'values' and 'values' are incompatible. Type 'IFormInputs' is not assignable to type '{ [x: string]: never; salutation: string; firstName: string; lastName: string; companyName: string; address: { [x: string]: never; street: string; addressComplement: string; zip: string; city: string; country: string; }; ... 16 more ...; concentrator: boolean | undefined; }'. Types of property 'address' are incompatible. Type '{ street: string; addressComplement: string; zip: string; city: string; country: string; }' is not assignable to type '{ [x: string]: never; street: string; addressComplement: string; zip: string; city: string; country: string; }'. Property 'street' is incompatible with index signature. Type 'string' is not assignable to type 'never'. TS2322

Schema definition: https://codesandbox.io/s/testhookformresolvers-meelj

👋🏻 You can try the last version v1.3.6. The issue should be solved

Hi @ljharb 👋🏻

If we revert back to v1.2.x a new problem will occur for users which use webpacker. Possible work around: https://github.com/react-hook-form/resolvers/issues/71#issuecomment-754623998

The V2 fix all of these issues. We are running the V2 beta since months with success, I think we can apply the same fix on the version 1. I keep an eye on this issue and I’ll fix V1 on the weekend

As a reminder:

  • Starting at Resolvers@v2.0.0-beta.7,React Hook Form V7 is required
  • We’ll continue to support the resolvers v1 for React Hook Form v6

@william-hotmart @jorisre Thanks both! Will this new configuration be necessary in v2?

Hey! I had the same issue and can confirm it’s now fixed. However, I am seeing this error message when running unit tests.

● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    /home/runner/work/webapp/webapp/node_modules/@hookform/resolvers/dist/yup.js:2
    import { transformToNestObject } from 'react-hook-form';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
      at Object.<anonymous> (node_modules/@hookform/resolvers/yup.js:1:107)

Has something else changed since 1.3.0? Until then everything worked fine, this happens with 1.3.2.

  • So should all the values in FormData also be in the schema? If my form contain a value that I do not need to validate (like colorBackground), how to proceed?
  • Can’t I use TS enum as a type in the useForm generic interface?

Thanks!

You get this error because your FormData contains more properties than your schema.

type MySchema = yup.InferType<typeof schema>;

You can see MySchema and FormData are different, that’s why you get that error.

You’ve a second TS error because you’re using TypeScript enum but yup.InferType could return an enum:

const schema = object({
  exibitionMode: string().oneOf(["infinity", "limited"]).required(),
});

type Schema = InferType<typeof schema>;
// Schema['exibitionMode'] = string instead of 'infinity' | 'limited'

I forgot. Sorry! "yup": 0.32.8" and "react-hook-form: 6.13.1"