- yup 0.30.0
- @types/yup 0.29.14
Struggling to create a reusable type definition for a Yup validationSchema
with ObjectSchema
resulting in an error.
Attempting to follow an example from the Yup documentation provided at: https://github.com/jquense/yup#ensuring-a-schema-matches-an-existing-type
interface TestValidationSchema {
title: string;
}
const validationSchema: Yup.ObjectSchema<TestValidationSchema> = Yup.object({
title: Yup.string().required()
});
...
return (
<Formik
initialValues={initialValues}
onSubmit={handleSubmit}
validationSchema={validationSchema}
>
...
Encountering this error:
Type 'ObjectSchema<Shape<object | undefined, { title: string; }>, object>' is not assignable to type 'ObjectSchema<TestValidationSchema, object>'.
Types of property 'fields' are incompatible.
Type '{ title: Schema<string, object>; } | undefined' is not assignable to type '{ title: Schema<string, object>; }'.
Type 'undefined' is not assignable to type '{ title: Schema<string, object>; }'.
Updated to version 32.11 of Yup
and encountered a different but still puzzling error related to incorrect usage of ObjectSchema
.
Type 'OptionalObjectSchema<{ title: RequiredStringSchema<string | undefined, AnyObject>; }, AnyObject, TypeOfShape<{ title: RequiredStringSchema<string | undefined, AnyObject>; }>>' is not assignable to type 'ObjectSchema<TestValidationSchema, AnyObject, TypeOfShape<TestValidationSchema>, AssertsShape<TestValidationSchema>>'.
The types of 'fields.title' are incompatible between these types.
Type 'RequiredStringSchema<string | undefined, AnyObject>' is not assignable to type 'string'.ts(2322)
Seeking assistance on resolving this issue.
UPDATE
Upon inspecting the type returned from the above validationSchema
, I found that the following type works. Still curious as to why the Yup documentation example isn't functioning correctly for me:
type ValidationSchema = Yup.ObjectSchema<{
title: string;
} | undefined, object>