简体   繁体   中英

How to type a function with parameters in typescript?

I have these interfaces and functions to manipulate data (email, password and, name) to send to my API.

interface SignUpData {
        email: string
        password: string
        name: string
    }

interface SignupProps {
    handleSignup: () => void
    errors: DeepMap<FieldValues, FieldError>
    register: UseFormRegister<FieldValues>

}

const handleSignup = ({ name, email, password }: SignUpData) => {
        api
        .post("/users", {name, email, password})
}

<SignupForm
                 
                errors={errors}
                register={register}
                handleSignup={handleSubmit(handleSignup)}
            />

However, I am getting a problem in my last line

handleSignup={handleSubmit(handleSignup)}

HandleSubmit is default of forms

It happens because:

Argument of type '({ name, email, password }: SignUpData) => void' is not assignable to parameter of type 'SubmitHandler'. Parameter types '__0' and 'data' are incompatible. The type '{ [x: string]: any; }' does not have the following properties of type 'SignUpData': email, password, namets(2345)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM