Skip to content

Commit

Permalink
Merge pull request #12649 from Automattic/vkarpov15/gh-12420
Browse files Browse the repository at this point in the history
fix(types): make array paths optional in inferred type of array default returns undefined
  • Loading branch information
vkarpov15 committed Nov 2, 2022
2 parents f9501d2 + 1f6864f commit 64f6ff4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions test/types/schema.test.ts
Expand Up @@ -412,6 +412,7 @@ export function autoTypedSchema() {
array5: any[];
array6: string[];
array7?: string[];
array8?: string[];
decimal1?: Types.Decimal128;
decimal2?: Types.Decimal128;
decimal3?: Types.Decimal128;
Expand Down Expand Up @@ -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'
Expand Down
8 changes: 7 additions & 1 deletion types/inferschematype.d.ts
Expand Up @@ -60,6 +60,12 @@ declare module 'mongoose' {
: unknown;
}

type IsPathDefaultUndefined<PathType> = 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.
Expand All @@ -69,7 +75,7 @@ type IsPathRequired<P, TypeKey extends TypeKeyBaseType = DefaultTypeKey> =
P extends { required: true | [true, string | undefined] } | ArrayConstructor | any[]
? true
: P extends (Record<TypeKey, ArrayConstructor | any[]>)
? P extends { default: undefined }
? IsPathDefaultUndefined<P> extends true
? false
: true
: P extends (Record<TypeKey, any>)
Expand Down

0 comments on commit 64f6ff4

Please sign in to comment.