Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript Yup | Expected 0 arguments, but got 1.ts(2554)

I am trying to create my ValidationSchemas using TS, React in order to use them in a Formik form.

I have multiple validation schemas I need to comply with in my module, so I thought of creating an enum, in order to easily reuse them.

IN A TS File:

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'),
  }),
}

But I get errors in every Yup.String, that say:

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

I am trying to find reference to the Yup types, but no luck. What is the problem here. I need to pass an argument to Yup, and it says, that is expected 0. Doesn't make sense to me. Thanks for your time!


2 Answers

Looking at your error message and the types for for yup here, it seems the problem is the calls you're making like this: Yup.string('Please select a group'). It seems the StringSchemaConstructor interface doesn't define a property which takes a string.

like image 167
Hayden Hall Avatar answered Oct 30 '25 00:10

Hayden Hall


Use below code for string error

username: Yup.string().required('Username is required').typeError('Provide a username')
like image 38
Muhammed Javad Avatar answered Oct 30 '25 02:10

Muhammed Javad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!