rubber-blueR
Refine3y ago
4 replies
rubber-blue

Add Type to useForm

How do I make the object that I am manipulating, with the help of useForm hook, type safe?

For instance, I know I can do things like:
<TextField
          {...register("email", {
            required: "This field is required",
            pattern: {
              value: /\S+@\S+\.\S+/,
              message: "Please enter a valid email address",
            },
          })}
          error={!!(errors as any)?.email}
          helperText={(errors as any)?.email?.message}
          margin="normal"
          fullWidth
          InputLabelProps={{ shrink: true }}
          type="email"
          label={"Email"}
          name="email"
        />


But how do i ensure email is type-safe with respect to some interface? For instance, I have a user interface that I would like to apply to my useForm, so that way I make sure I only perform the proper useForm actions on the fields that exist for user
Was this page helpful?