
[英]Type 'unknown' is not assignable to type '(prevState: number | undefined) => number | undefined'
[英]Type 'undefined' is not assignable to type 'Schema<unknown>'. Yup
我不确定我是否收到此错误,有人知道如何解决这个问题吗? 下面是我的代码,错误发生在extra: Yup.array().of(Yup.lazy((value)....
const claimFormValidationSchema = Yup.object().shape<ClaimApplicationForm>({
id: Yup.number().notRequired(),
claimType: Yup.mixed().required(),
claimAmount: Yup.number().required('Claim Amount is Required'),
currencyCode: Yup.string().required(),
rate: Yup.number().required('Rate is Required'),
convertedAmount: Yup.number().required('Converted Amount is Required'),
extra: Yup.array().of(
Yup.lazy((value) => {
if ('required' in value) {
if (value.required === true) {
return Yup.object().shape({
value: Yup.string().required(),
});
} else {
return Yup.object().shape({
value: Yup.string().notRequired(),
});
}
}
}),
),
});
错误:
'(值:未知)类型的参数 => ObjectSchema> | undefined' 不能分配给类型为 '(value: unknown) => Schema' 的参数。 键入“对象架构> | undefined' 不可分配给类型 'Schema'。 类型“未定义”不可分配给类型“架构”。
你的问题出在extra
财产上
extra: Yup.array().of(
Yup.lazy((value) => {
// outter if
if ('required' in value) {
if (value.required === true) {
return Yup.object().shape({
value: Yup.string().required(),
});
} else {
return Yup.object().shape({
value: Yup.string().notRequired(),
});
}
}
// end outter if
// if reaches here, nothing is returned
}),
),
你需要在Yup.lazy
中返回一些东西,但是如果这个条件不是 true 'required' in value
,你将不会返回任何东西,给你undefined
和那个错误。
我不确定你需要返回什么,但它一定是一些东西。
一段时间后,我得到了我期望做的事情。
extra: Yup.array().of(
Yup.lazy((data: any) => {
if ('required' in data) {
if (data.required === true) {
return Yup.object().shape({
value: Yup.string().required(),
});
} else {
return Yup.object().shape({
value: Yup.string().notRequired(),
});
}
}
}),
),
我通过像这样为“额外”设置架构来解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.