react-hook-form conditional error message typing

I’m fairly new to using Typescript and I’m having a go at setting up Auth in my first ts React project. I’ve successfully set up validation using react-hook-form in an AuthForm.tsx component that renders the Login form and Sign Up form interchangeably.

I’ve set it up to map over input field data for each form that I’ve stored as objects in a separate constants.ts.

Currently, when I try to access the errorMessages[name] in the Sign Up iteration of the form I’m getting an error stating that the errors have been implicitly typed any due to a FieldError conflict.

I’ve been poking around the react-hook-form documentation on FieldErrors and am feeling a bit out of my depth. Has anyone had a similar issue building a reusable form component in Typescript?

Error:

Element implicitly has an 'any' type because expression of type 
'"username" | "password" | "confirm"' can't be used to index type 'FieldErrors<Inputs>'.
Property 'confirm' does not exist on type 'FieldErrors<Inputs>'.

AuthForm.tsx

import H1 from "./H1";
import { loginInputFieldData, signupInputFieldData } from "../lib/constants";
import { useAuthFormToggle } from "../lib/hooks";
import { SubmitHandler, useForm } from "react-hook-form";
import { LoginFormInputs, SignUpFormInputs } from "../lib/types";
import { hasRegexErrorMessage } from "../lib/utils";

type Inputs = LoginFormInputs | SignUpFormInputs;

export default function AuthForm() {
  const { formType, handleAuthFormToggle } = useAuthFormToggle();

  const {
    register,
    watch,
    handleSubmit,
    formState: { errors },
  } = useForm<Inputs>();

  const onSubmit: SubmitHandler<Inputs> = (data) => console.log(data);

  return (
    <form
      onSubmit={handleSubmit(onSubmit)}
      className="flex flex-col space-y-4 items-center mx-2"
    >
      <H1 className="text-center text-lg">
        {formType === "login" ? "Log In" : "Sign Up"}
      </H1>

      {formType === "login"
        ? loginInputFieldData.map(
            ({ type, placeholder, name, errorMessages }) => (
              <div key={name}>
                <input
                  {...register(name, { required: errorMessages.required })}
                  type={type}
                  placeholder={placeholder}
                  className="bg-secondary h-9 border border-primary border-opacity-20 rounded-md focus:outline-none focus:ring-2 focus:ring-primary focus:ring-opacity-45 px-2"
                />
                {errors[name] && (
                  <p className="text-red-500">{errors[name]?.message}</p>
                )}
              </div>
            )
          )
        : signupInputFieldData.map(
            ({ type, placeholder, name, errorMessages }) => (
              <div>
                <input
                  {...register(name as keyof SignUpFormInputs, {
                    required: errorMessages.required,
                    pattern: hasRegexErrorMessage(errorMessages)
                      ? {
                          value:
                            /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/,
                          message: errorMessages.regEx,
                        }
                      : undefined,
                    validate: (val: string) => {
                      if (watch("password") != val) {
                        return errorMessages.mismatch;
                      }
                    },
                  })}
                  type={type}
                  placeholder={placeholder}
                  className="bg-secondary h-9 border border-primary border-opacity-20 rounded-md focus:outline-none focus:ring-2 focus:ring-primary focus:ring-opacity-45 px-2"
                />
                {errors[name] && (
                  <p className="text-sm text-red-500">
                    {errors[name]?.message}
                  </p>
                )}
              </div>
            )
          )}

      <input
        type="submit"
        className="bg-primary text-white rounded-md px-5 py-2 hover:bg-blue-800 transition duration-75"
      />

      <hr className="w-full border-t border-gray-300 opacity-65" />

      <p className="text-xs opacity-50">
        {formType === "login" ? "Or" : "Already signed up?"}{" "}
        <span
          onClick={handleAuthFormToggle}
          className="text-bold underline cursor-pointer hover:opacity-75"
        >
          {formType === "login" ? "create an account" : "Login"}
        </span>
      </p>
    </form>
  );
}

constants.ts

type TLoginInputFieldData = {
  type: string;
  placeholder: "Username" | "Password";
  name: "username" | "password";
  errorMessages: {
    required: string;
  };
};

export const loginInputFieldData: TLoginInputFieldData[] = [
  {
    type: "text",
    placeholder: "Username",
    name: "username",
    errorMessages: { required: "Username is required." },
  },
  {
    type: "password",
    placeholder: "Password",
    name: "password",
    errorMessages: {
      required: "Password is required.",
    },
  },
];

type TSignUpInputFieldData = {
  type: string;
  placeholder: "Username" | "Password" | "Confirm password";
  name: "username" | "password" | "confirm";
  errorMessages: {
    required: string;
    regEx?: string;
    mismatch?: string;
  };
};

export const signupInputFieldData: TSignUpInputFieldData[] = [
  {
    type: "text",
    placeholder: "Username",
    name: "username",
    errorMessages: { required: "Username is required." },
  },
  {
    type: "password",
    placeholder: "Password",
    name: "password",
    errorMessages: {
      required: "Password is required.",
      regEx:
        "Password must have a minimum of 8 characters.nPassword must contain one uppercase letter.nPassword must contain one lowercase letter.nPassword must contain one digit 0-9.nPassword must contain one special character.",
    },
  },
  {
    type: "password",
    placeholder: "Confirm password",
    name: "confirm",
    errorMessages: {
      required: "Password confirmation is required.",
      mismatch: "Password and confirmation must match.",
    },
  },
];

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật