Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type inference for react hook form #2238

Open
adenteo opened this issue Jul 28, 2024 · 0 comments
Open

Type inference for react hook form #2238

adenteo opened this issue Jul 28, 2024 · 0 comments

Comments

@adenteo
Copy link

adenteo commented Jul 28, 2024

With the example code below,

    interface ITest {
        test?: string;
        test2?: string;
    }

    const testObj: yup.ObjectSchema<ITest> = yup.object().shape({
        test: yup.string().required(),
        test2: yup.string().required(),
    });

    const methods = useForm({
        mode: "onTouched",
        resolver: yupResolver(testObj),
    });

I am trying to enforce the interface ITest on testObj. However, with yup.ObjectSchema<ITest>, when i try to infer the available parameters with methods.watch(), I am unable to get them. If i remove yup.ObjectSchema<ITest>, then It works. Is there something I am doing wrong? How else do I enforce my schema type while still ensuring that I can infer the types correctly?

SCENARIO 1, WITHOUT yup.ObjectSchema<ITest>:

const testObj = yup.object().shape({
        test: yup.string().required(),
        test2: yup.string().required(),
    });
Screenshot 2024-07-28 at 10 14 24 PM Screenshot 2024-07-28 at 10 17 20 PM

SCENARIO 2, WITH yup.ObjectSchema<ITest> I lose all type inference.

const testObj: yup.ObjectSchema<ITest> = yup.object().shape({
        test: yup.string().required(),
        test2: yup.string().required(),
    });
Screenshot 2024-07-28 at 10 21 38 PM Screenshot 2024-07-28 at 10 19 31 PM

How do I achieve scenario 1 but still have the type enforcement with yup.ObjectSchema<ITest>?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant