简体   繁体   English

如何在typescript中输入一个function的参数?

[英]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.我有这些接口和函数来处理数据(电子邮件、密码和姓名)以发送到我的 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)} handleSignup={handleSubmit(handleSignup)}

HandleSubmit is default of forms HandleSubmit 默认为 forms

It happens because:它发生是因为:

Argument of type '({ name, email, password }: SignUpData) => void' is not assignable to parameter of type 'SubmitHandler'. “({ name, email, password }: SignUpData) => void”类型的参数不可分配给“SubmitHandler”类型的参数。 Parameter types '__0' and 'data' are incompatible.参数类型“__0”和“数据”不兼容。 The type '{ [x: string]: any;类型 '{ [x: string]: any; }' does not have the following properties of type 'SignUpData': email, password, namets(2345) }' 不具有 'SignUpData' 类型的以下属性:email、密码、namets(2345)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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