You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to type the register prop to accept register function from any form as long as this form has thing: Thing property. In my understanding the way above should work:
We have a generic type constraint what is ensuring that the form must have the thing prop
This generic type T is passed to type the register function using UseFormRegister type
This generic type T is passed to type the FieldErrors type with generic
register function is correctly typed, so the name argument must be a keyof of the Thing type
However, this is not working. When I'm trying to use register('thing.nestedProp'), the following ts error is being thrown:
Argument of type '"thing.nestedProp"' is not assignable to parameter of type 'Path<T>'.ts(2345)
The same goes for the error. Using errors.thing?.nestedProp?.message gives standard Prop Does Not Exist error.
Property 'nestedProp' does not exist on type 'FieldError'.ts(2339)
where Path<T> should evaluate to Path<{} & thing: Thing>
definition of Path
Type which eagerly collects all paths through a type
@typeParam — T - type which should be introspected
@example
Path<{foo: {bar: string}}> = 'foo' | 'foo.bar'
I am able to resolve these errors by casting exactly to ThingFormValues.
const parsedErrors = errors as FieldErrors<ThingFormValues>;
However, I'm wondering - why this is not working in the first place? How (except casting) can I achieve that?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I have this
ThingForm.tsx
, which is a separated part of the bigger form for reusability purposes.I want to type the
register
prop to acceptregister
function from any form as long as this form hasthing: Thing
property. In my understanding the way above should work:thing
propregister
function usingUseFormRegister
typeFieldErrors
type with genericregister
function is correctly typed, so thename
argument must be akeyof
of theThing
typeHowever, this is not working. When I'm trying to use
register('thing.nestedProp')
, the followingts
error is being thrown:The same goes for the
error
. Usingerrors.thing?.nestedProp?.message
gives standard Prop Does Not Exist error.where
Path<T>
should evaluate toPath<{} & thing: Thing>
definition of
Path
I am able to resolve these errors by casting exactly to
ThingFormValues
.However, I'm wondering - why this is not working in the first place? How (except casting) can I achieve that?
Beta Was this translation helpful? Give feedback.
All reactions