TypeScript是预期有0个参数,但得到了1.ts(2554)

时间:2019-04-11 11:31:06

标签: reactjs typescript schema formik yup

我试图使用TS,React创建我的ValidationSchema,以便以Formik形式使用它们。

我的模块中需要遵循多个验证模式,因此我想创建一个枚举,以便于轻松地重用它们。

在TS文件中:

import * as Yup from "yup";

export const ValidationSchema {
  AddUserSchema = Yup.object().shape({
    username: Yup.string('Provide a username').required('Username is required'),
    email: Yup.string().email('Provide a valid email address'),
    password: Yup.string('Provide a password').required('Password is required'),
    confirmPassword: Yup.string('Provide your password again')
      .required('Password confirmation is required')
      .oneOf([Yup.ref('password')], 'Passwords do not match'),
    group: Yup.string('Please select a group').required('Group is required')
  }),
  EditUserSchema = Yup.object().shape({
    username: Yup.string('Provide a username').required('Username is required'),
    email: Yup.string().email('Provide a valid email address'),
    group: Yup.string('Please select a group').required('Group is required')
  }),
  EditPasswordSchema = Yup.object().shape({
    password: Yup.string('Provide a password').required('Password is required'),
    confirmPassword: Yup.string('Provide your password again')
      .required('Password confirmation is required')
      .oneOf([Yup.ref('password')], 'Passwords do not match'),
  }),
}

但是我在每个Yup.String中都遇到错误,说:

Expected 0 arguments, but got 1.ts(2554)
const string: Yup.StringSchemaConstructor
() => Yup.StringSchema

我试图找到对Yup类型的引用,但是没有运气。这里有什么问题。我需要将一个参数传递给Yup,它说应该是0。对我来说这没有意义。谢谢您的宝贵时间!

2 个答案:

答案 0 :(得分:0)

查看您的错误消息和yup here的类型,看来问题在于您正在像这样进行呼叫:Yup.string('Please select a group')。似乎StringSchemaConstructor接口未定义接受string的属性。

答案 1 :(得分:0)

这是因为 Typescript 兼容性。使用 Yup.string() 。更多详情请访问:Yup Typescript