Skip to content

Commit 80f597f

Browse files
committed
fix(types): make array paths optional in inferred type of array default returns undefined
Fix #12420
1 parent fd2d826 commit 80f597f

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

test/types/schema.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,3 +870,15 @@ function gh12431() {
870870
type Example = InferSchemaType<typeof testSchema>;
871871
expectType<{ testDate?: Date, testDecimal?: Types.Decimal128 }>({} as Example);
872872
}
873+
874+
function gh12420() {
875+
const TestSchema = new Schema(
876+
{
877+
comments: { type: [String], default: () => undefined }
878+
}
879+
);
880+
881+
expectType<{
882+
comments?: string[]
883+
}>({} as InferSchemaType<typeof TestSchema>);
884+
}

types/inferschematype.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ declare module 'mongoose' {
6060
: unknown;
6161
}
6262

63+
type IsPathDefaultUndefined<PathType> = PathType extends { default: undefined } ?
64+
true :
65+
PathType extends { default: (...args: any[]) => undefined } ?
66+
true :
67+
false;
68+
6369
/**
6470
* @summary Checks if a document path is required or optional.
6571
* @param {P} P Document path.
@@ -69,7 +75,7 @@ type IsPathRequired<P, TypeKey extends TypeKeyBaseType = DefaultTypeKey> =
6975
P extends { required: true | [true, string | undefined] } | ArrayConstructor | any[]
7076
? true
7177
: P extends (Record<TypeKey, ArrayConstructor | any[]>)
72-
? P extends { default: undefined }
78+
? IsPathDefaultUndefined<P> extends true
7379
? false
7480
: true
7581
: P extends (Record<TypeKey, any>)

0 commit comments

Comments
 (0)