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)