reactjs - 是的,是的,是的,是的,字符串架构不可分配给类型“字符串”
问题描述
对于电话号码验证中的项目或选项值,我在 tsx 中遇到类型错误。是的,抛出一个类型错误,说参数类型
'(_item: any, options: { path: string;}) => yup.StringSchema' 不能分配给'(value: string) => Schema' 类型的参数。
这是我的代码:
export const schema = yup.object().shape({
contactID: yup.string().required('Please select an option').nullable(),
firstName: yup.string().when('contactID', {
is: 'other',
then: yup.string().required('First Name is required').nullable(),
otherwise: yup.string().nullable(),
}),
lastName: yup.string().when('contactID', {
is: 'other',
then: yup.string().required('Last Name is required').nullable(),
otherwise: yup.string().nullable(),
}),
phoneNumber: yup.array().of(
yup.object().shape({
phoneNumber: yup.lazy((item: any, options: { path: string }) => {
if (options.path === 'insuredContactInformation.phoneNumber[0].phoneNumber') {
return phoneValidation(true).required('Phone Number is required');
} else return phoneValidation(true);
}),
phoneType: yup.string().nullable()
})
),
});
这是错误
解决方案
我设法通过在传递给组件时更改 PropTypes 类型来解决它。而不是object,我通过func:
// Proptypes
BrandForm.propTypes = {
t: PropTypes.func.isRequired,
onFormSubmit: PropTypes.func.isRequired,
validationSchema: PropTypes.func.isRequired,
};
当以这种方式传递给组件时:
<BrandForm
validationSchema={() => addNewBrandSchema(language)}
/>
推荐阅读
- javascript - 高阶组件总是在 ReactJS 中返回相同的结果
- highcharts - highchart - 如何处理标志重叠问题?
- websphere - jython wsadmin 脚本更改为 dmgr jvm
- android - 使用 FragmentPagerAdapter 刷新片段中的数据
- r - R roxygen2安装错误,与libicui18n有关
- javascript - 如何检查年龄是否在特定年份之间的范围内?
- ios - 无法让 Popup 视图控制器在 iPhone 上工作
- ruby - 使用 capybara-screenshot gem 中的 screenshot_and_open_image 方法;我希望能够指定要保存的文件名
- jsp - 选择条件在jsp中显示表格
- react-native - 来自 React Native 的 TextInput 不显示输入的文本