vuvu
vuvu

Reputation: 5328

Type definition does not seem to work in react-hook-form 7

I defined the types of my form fields. I have have 3 different fields, with 3 different types. After initiating the form each field seems to be valuable with all 3 different types. What is wrong?

enter image description here

Sandbox example

type Inputs = {
  val1: string
  val2: number
  val3: boolean
};

mouseover types shows:

const val1: string | number | boolean

Upvotes: 0

Views: 158

Answers (2)

BTiers
BTiers

Reputation: 26

@vuvu This behaviour has been fixed in version 7.0.6 of react-hook-form and types are now properly retrieved. (see this PR for reference)

Upvotes: 1

Nimna Perera
Nimna Perera

Reputation: 1065

@vuvu Your code is working perfectly. The behavior you got is happening because of watch is returning a array. Inside a array if you placed items with different types then you can have items with those all types inside the array. You can read this tutorial as an additional reading material.

As it says,

Of course, you can always initialize an array like shown below, but you will not get the advantage of TypeScript's type system.

let arr = [1, 3, 'Apple', 'Orange', 'Banana', true, false];

Upvotes: 1

Related Questions