Broken typing
#8617
Replies: 1 comment 3 replies
-
You should pass the register("fieldName", { valueAsNumber: true }) There's no way to detect the input's |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm not sure how to qualify this issue. I didn't find something similar in the issues or in the discussions, which surprise me, so maybe I missed it.
I'm used to type everything possible in my code with TypeScript to catch as many errors as possible at compile time.
But there is a big problem in the code below: the
score
value of typenumber
got silently transformed as astring
when I edit the field!I get it: the
register
takes the string value of the input, and store it in the object attributescore
. Fine. But what worries me is that TypeScript is unable to detect this mistake, and the typing get broken since I end up with astring
in a field typed asnumber
.Sure I could add a
setValueAs
in theregister
to convert the value, but I've yet to detect the mistake if it happens.How could I strengthen the typing to prevent that?
When running the code below, this displays
42
initialy, then"42"
(or something else) if I edit / blur the field, showing that my value got converted to a type that is inconsistent with the TypeScript definition.Beta Was this translation helpful? Give feedback.
All reactions