ค้นหาเว็บไซต์

reactjs - ฟังก์ชัน register ของ React-hook-form แสดงข้อผิดพลาด Type argument cannot be inferred from usage ใน TypeScript


ฉันกำลังสร้างแอปพลิเคชัน React JS และฉันกำลังใช้ปลั๊กอิน React hook form สำหรับจัดการค่าของแบบฟอร์ม https://react-hook-form.com/ ฉันกำลังใช้มันกับ TypeScript แต่มันแสดงข้อผิดพลาดให้ฉัน

นี่คือโค้ดของฉัน

import { useForm } from "react-hook-form";

type LoginForm = {
    email: string;
    password: string;
}


export const Login = () => {
    const { register, handleSubmit, watch, formState: { errors } } = useForm<LoginForm>();
    const onSubmit = data => console.log(data);

    return (
        /* "handleSubmit" will validate your inputs before invoking "onSubmit" */
        <form onSubmit={handleSubmit(onSubmit)}>
            {/* register your input into the hook by invoking the "register" function */}
            <input defaultValue="test" {...register("email")} />

            {/* include validation with required or other standard HTML validation rules */}
            <input {...register("password", { required: true })} />
            {/* errors will return when field validation fails  */}
            {errors.password && <span>This field is required</span>}

            <input type="submit" />
        </form>
    );
}

ใน IDE ฟังก์ชันลงทะเบียนกำลังแสดงข้อผิดพลาดนี้

Type argument cannot be inferred from usage 

โค้ดของฉันมีปัญหาอะไรและฉันจะแก้มันได้อย่างไร?

คำตอบ #1

ลองรีสตาร์ท IDE ของคุณ บางครั้งมันอาจเกิดข้อบกพร่องหลังจากการติดตั้งใหม่

บทความที่เกี่ยวข้อง