From ebe610ebbd91c147dcbdcb31a258b8da3e0044ff Mon Sep 17 00:00:00 2001 From: Luca Pizzini Date: Sat, 17 Sep 2022 16:20:42 +0200 Subject: [PATCH] Fixed InferSchemaType nullable arrays type inference fixes #12420 --- test/types/schema.test.ts | 29 +++++++++++++++++++++++++++++ types/inferschematype.d.ts | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) 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)