Here is a sample schema using ajv (v8.11.2)
import Ajv, { JSONSchemaType } from "ajv";
interface MyType {
myProp?: OtherType;
}
interface OtherType {
foo: string;
bar: number;
}
const otherSchema: JSONSchemaType<OtherType> = {
type: 'object',
properties: {
foo: { type: 'string', minLength: 1 },
bar: { type: 'number' },
},
required: ['foo', 'bar'],
additionalProperties: false
};
const mySchema: JSONSchemaType<MyType> = {
type: 'object',
properties: {
myProp: otherSchema,
},
required: [],
additionalProperties: false,
};
An error occurs:
Types of property '$ref' are incompatible. Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.
The issue may be due to TypeScript not recognizing that myProp
in mySchema
could be undefined even though it is not in the required list.
Any suggestions on how to resolve this schema error?