Can I validate Yup type using typescript interface in react-hook-form?

Viewed 476

I tried to validate Yup object with typescript like below.

        interface IYup {
        num: number[],
        str: string
        }
    
        const validationSchema = Yup.object().shape({
            num: Yup.array(),
            str: Yup.string()
        })
    
        const methods = useForm<IYup>({resolver: yupResolver<Yup.AnyObjectSchema>(validationSchema)});
        const {register, handleSubmit} = methods;

If I write code like below, I want to make type error occur

Type '{ num: array; str: number; }' is not assignable to type 'IYup'.
Object literal may only specify known properties, and 'number' does not exist in type 'IYup'.

        interface IYup {
        num: number[],
        str: string
        }
    
        const validationSchema = Yup.object().shape({
            num: Yup.array(),
            str: Yup.number()
        })
    
        const methods = useForm<IYup>({resolver: yupResolver<Yup.AnyObjectSchema>(validationSchema)});
        const {register, handleSubmit} = methods;

How can I make type error occur using typescript?

I found potential answers in yup document, but it didn't work as I expected.

https://github.com/jquense/yup#typescript-integration

0 Answers
Related