【问题标题】:Yup validation yup string schema is not assignable to type 'string'是的,是的,是的,字符串模式不能分配给类型“字符串”
【发布时间】:2021-06-25 19:47:13
【问题描述】:

电话号码验证中的项目或选项值在 tsx 中出现类型错误。是的,它抛出了一个类型错误,说参数类型

'(_item: any, options: { path: string;}) => yup.StringSchema' 不可分配给类型参数 '(值:字符串)=> 架构'。

这是我的代码:

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()
    })
  ),
});

这是错误

【问题讨论】:

    标签: reactjs typescript yup


    【解决方案1】:

    我设法通过在传递给组件时更改 PropTypes 类型来解决它。我通过 func 代替 object

    // Proptypes
    BrandForm.propTypes = {
      t: PropTypes.func.isRequired,
      onFormSubmit: PropTypes.func.isRequired,
      validationSchema: PropTypes.func.isRequired,
    };
    

    当以这种方式传递给组件时:

      <BrandForm
          validationSchema={() => addNewBrandSchema(language)}
      />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-26
      • 1970-01-01
      • 2021-10-21
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多