我可以使用React-Hook-Form中的Typescript接口验证YUP类型吗?
我试图用类似于下面的打字稿验证yup对象。
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;
如果我像下面的代码一样编写代码,我想发生类型错误
类型'{num:array; str:数字; }'不能分配给'iyup'。
对象文字只能指定已知属性,而“数字”不得 存在于类型的“ 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;
如何使用TypeScript发生类型错误?
我在YUP文档中找到了潜在的答案,但是我没有奏效 预期。
https://github.com/jquense/jquense/yup#typescript-integration
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论