diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index c65b3bfdd9c..bb0a6970839 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -741,3 +741,32 @@ function pluginOptions() { schema.plugin(pluginFunction2, { option2: 0 }); expectError(schema.plugin(pluginFunction2, {})); // should error because "option2" is not optional } + +function gh12420() { + const TestSchema = new Schema( + { + comments: { type: [String], required: false }, + reference: { + type: [ + { + type: { type: String, required: false }, + value: { type: String, required: false } + } + ], + required: false + } + }, + { + collection: 'tests', + timestamps: false + } + ); + + expectType<{ + comments?: string[], + reference?: { + type?: string, + value?: string + }[] + }>({} as InferSchemaType); +} diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index 5041f830477..e951012692c 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -69,7 +69,7 @@ type IsPathRequired = P extends { required: true | [true, string | undefined] } | ArrayConstructor | any[] ? true : P extends (Record) - ? P extends { default: undefined } + ? P extends { default: undefined } | { required: false } ? false : true : P extends (Record)