- yup 0.30.0
- @types/yup 0.29.14
I'm trying to generate a reusable type definition for a Yup validationSchema
by using ObjectSchema
but I keep getting an error
Using an example from the Yup docs here 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}
>
...
The 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>; }'.
I tried bumping the version of Yup
to 32.11 and got a different, but still confusing error that makes me think I'm just using ObjectSchema
incorrectly
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)
Any help is appreciated.
UPDATE
I just inspected the type returned from the validationSchema
above, and the below type works. I'm still wondering why the example in the Yup docs isn't working for me though
type ValidationSchema = Yup.ObjectSchema<{
title: string;
} | undefined, object>