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'm integrating RHF with Zod and encountering a TypeScript error that's been a bit challenging to resolve.
My use case involves using a form to either 'create' a resource or 'update' it. Although this is a common scenario, my schemas for both 'create' and 'update' are distinct.
Here's a brief representation of the schemas:
CreateSchema
constcreateFormSchema=z.object({firstName: z.string(),age: z.string(),hiddenField: z.string().default('this is a hidden field'),// exclusive to the create schema});
UpdateSchema
constupdateFormSchema=z.object({id: z.string().default('some-id'),// exclusive to the update schemafirstName: z.string(),age: z.string(),});
Both schemas share the firstName and age fields. Even though they have unique fields (hiddenField for create and id for update), the form's UI remains consistent.
To maximize code reuse, I've devised a BaseForm component for both creation and updating. This component accepts register and errors as props, both originating from RHF.
This approach seemed to be functional until I tried integrating this generic form within either the CreateForm or UpdateForm component. At this point, TypeScript raised errors about missing properties.
I was surprised that register would demand a field that is specific to only one of the schemas (like hiddenField).
Could anyone guide me on what might be amiss? I'm contemplating if separate schemas might be inappropriate for this scenario. Perhaps using a discriminated union might be a better alternative? (Although, I'm not entirely sure 🤔)
I've set up a minimal reproduction and a related PR for a clearer understanding:
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.
-
Hello everyone!
I'm integrating RHF with Zod and encountering a TypeScript error that's been a bit challenging to resolve.
My use case involves using a form to either 'create' a resource or 'update' it. Although this is a common scenario, my schemas for both 'create' and 'update' are distinct.
Here's a brief representation of the schemas:
CreateSchema
UpdateSchema
Both schemas share the
firstName
andage
fields. Even though they have unique fields (hiddenField
for create andid
for update), the form's UI remains consistent.To maximize code reuse, I've devised a
BaseForm
component for both creation and updating. This component acceptsregister
anderrors
as props, both originating from RHF.This approach seemed to be functional until I tried integrating this generic form within either the
CreateForm
orUpdateForm
component. At this point, TypeScript raised errors about missing properties.I was surprised that
register
would demand a field that is specific to only one of the schemas (likehiddenField
).Could anyone guide me on what might be amiss? I'm contemplating if separate schemas might be inappropriate for this scenario. Perhaps using a discriminated union might be a better alternative? (Although, I'm not entirely sure 🤔)
I've set up a minimal reproduction and a related PR for a clearer understanding:
View the PR and Issue Here
Thank you for your assistance!
Beta Was this translation helpful? Give feedback.
All reactions