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
- fix: yup resolver TypeScript error (#106) Solve #97 — committed to react-hook-form/resolvers by jorisre 3 years ago
- test: add components test to ensure TypeScript is working as expected Related to #97 — committed to react-hook-form/resolvers by jorisre 3 years ago
- Fix: vest validate all criteria + better TypeSript support + add tests * test: add components test to ensure TypeScript is working as expected Related to #97 * chore: improve zod resolvers type... — committed to react-hook-form/resolvers by jorisre 3 years ago
- chore: 🤖 downgrading @hookform/resolvers https://github.com/react-hook-form/resolvers/issues/97 — committed to biodiv-platform/biodiv-ui by harshzalavadiya 3 years ago
- V2 🚀 (#108) * feat: V2 init BREAKING CHANGE: there will be some breaking changes in the next versions * feat: improve modules support * fix: yup resolver typescript errors * Fix: vest va... — committed to react-hook-form/resolvers by jorisre 3 years ago
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
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:
@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.
Has something else changed since
1.3.0
? Until then everything worked fine, this happens with1.3.2
.FormData
also be in the schema? If my form contain a value that I do not need to validate (likecolorBackground
), how to proceed?useForm
generic interface?Thanks!
You get this error because your
FormData
contains more properties than your schema.You can see
MySchema
andFormData
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:I forgot. Sorry!
"yup": 0.32.8"
and"react-hook-form: 6.13.1"