From 80f597f3b592f7918d1aa6931d405af7a09880cd Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 2 Nov 2022 10:58:27 -0400 Subject: [PATCH 1/2] fix(types): make array paths optional in inferred type of array default returns undefined Fix #12420 --- test/types/schema.test.ts | 12 ++++++++++++ types/inferschematype.d.ts | 8 +++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 86e4d2668a4..7ca2dfa90f1 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -870,3 +870,15 @@ function gh12431() { type Example = InferSchemaType; expectType<{ testDate?: Date, testDecimal?: Types.Decimal128 }>({} as Example); } + +function gh12420() { + const TestSchema = new Schema( + { + comments: { type: [String], default: () => undefined } + } + ); + + expectType<{ + comments?: string[] + }>({} as InferSchemaType); +} diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index a0406388f00..27090b89eb4 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -60,6 +60,12 @@ declare module 'mongoose' { : unknown; } +type IsPathDefaultUndefined = PathType extends { default: undefined } ? + true : + PathType extends { default: (...args: any[]) => undefined } ? + true : + false; + /** * @summary Checks if a document path is required or optional. * @param {P} P Document path. @@ -69,7 +75,7 @@ type IsPathRequired = P extends { required: true | [true, string | undefined] } | ArrayConstructor | any[] ? true : P extends (Record) - ? P extends { default: undefined } + ? IsPathDefaultUndefined

extends true ? false : true : P extends (Record) From 1f6864f04f362ea4692ab2685f6014e067bcb2df Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 2 Nov 2022 12:40:50 -0400 Subject: [PATCH 2/2] test: address some code review comments to streamline tests --- test/types/schema.test.ts | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 7ca2dfa90f1..2ab4698e4bc 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -412,6 +412,7 @@ export function autoTypedSchema() { array5: any[]; array6: string[]; array7?: string[]; + array8?: string[]; decimal1?: Types.Decimal128; decimal2?: Types.Decimal128; decimal3?: Types.Decimal128; @@ -458,6 +459,7 @@ export function autoTypedSchema() { array5: [], array6: { type: [String] }, array7: { type: [String], default: undefined }, + array8: { type: [String], default: () => undefined }, decimal1: Schema.Types.Decimal128, decimal2: 'Decimal128', decimal3: 'decimal128' @@ -870,15 +872,3 @@ function gh12431() { type Example = InferSchemaType; expectType<{ testDate?: Date, testDecimal?: Types.Decimal128 }>({} as Example); } - -function gh12420() { - const TestSchema = new Schema( - { - comments: { type: [String], default: () => undefined } - } - ); - - expectType<{ - comments?: string[] - }>({} as InferSchemaType); -}