I am currently facing a challenge in creating a TypeScript interface for a Yup schema (https://github.com/jquense/yup/blob/master/docs/typescript.md)
Here is the TypeScript interface I am working with:
interface TestInterface {
name: string;
};
The following return type is acceptable:
const testSchema: Yup.SchemaOf<TestInterface> = Yup.object().shape({
name: Yup.string().required()
});
However, in my code I am using Yup.lazy()
, and I am unsure how to create an interface for the following scenario:
const testSchema: Yup.SchemaOf<TestInterface> = Yup.object().shape({
name: Yup.lazy(() => Yup.string().required())
});
The error message I am encountering is:
Type 'ObjectSchema<Assign<ObjectShape, { name: Lazy<RequiredStringSchema<string, Record<string, any>>, Record<string, any>>; }>, Record<...>, TypeOfShape<...>, AssertsShape<...>>' is not assignable to type 'ObjectSchemaOf<TestInterface>'.
Type 'Assign<ObjectShape, { name: Lazy<RequiredStringSchema<string, Record<string, any>>, Record<string, any>>; }>' is not assignable to type '{ name: BaseSchema<string, AnyObject, string>; }'.
Types of property 'name' are incompatible.
Type 'Lazy<RequiredStringSchema<string, Record<string, any>>, Record<string, any>>' is missing the following properties from type 'BaseSchema<string, AnyObject, string>': deps, tests, transforms, conditions, and 35 more.ts(2322)