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
This is an idea I had and thought someone might find it interesting and put the time in to explore it further.
I've got a lot of duplication going on with my form: a Yup schema, a Typescript type, initial values, and the form itself. It seems like there should be a way to combine all or some of those and reduce the boilerplate.
const PersonSchema = Yup.object().shape({
name: Yup.string().default('John')
});
type Person = Yup.InferType<typeof PersonSchema>
const initialValues = YupSchema.cast({});
I ran into some roadblocks with my more complicated schema (enums and nested properties don't work right) but I'm optimistic that there is a solution out there to describe your schema once and generate the type and initial values. The first method will work with more complicated schemas but you need to cast them as the proper type.
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
-
This is an idea I had and thought someone might find it interesting and put the time in to explore it further.
I've got a lot of duplication going on with my form: a Yup schema, a Typescript type, initial values, and the form itself. It seems like there should be a way to combine all or some of those and reduce the boilerplate.
This works in regular Javascript:
In Typescript, the following seems to work:
I ran into some roadblocks with my more complicated schema (enums and nested properties don't work right) but I'm optimistic that there is a solution out there to describe your schema once and generate the type and initial values. The first method will work with more complicated schemas but you need to cast them as the proper type.
Beta Was this translation helpful? Give feedback.
All reactions