Skip to content

Commit b34d1d5

Browse files
committedSep 2, 2021
fix(index.d.ts): simplify UpdateQuery to avoid "excessively deep and possibly infinite" errors with extends Document and any
Fix #10617
1 parent 2a3399e commit b34d1d5

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed
 

‎index.d.ts

+28-28
Original file line numberDiff line numberDiff line change
@@ -2536,40 +2536,29 @@ declare module 'mongoose' {
25362536

25372537
type NumericTypes = number | mongodb.Decimal128 | mongodb.Double | mongodb.Int32 | mongodb.Long;
25382538

2539-
type KeysOfAType<TSchema, Type> = {
2540-
[key in keyof TSchema]: NonNullable<TSchema[key]> extends Type ? key : never;
2541-
}[keyof TSchema];
2542-
2543-
type PullOperator<TSchema> = {
2544-
[key in KeysOfAType<TSchema, ReadonlyArray<any>>]?:
2545-
| Partial<Unpacked<TSchema[key]>>
2546-
| mongodb.ObjectQuerySelector<Unpacked<TSchema[key]>>
2547-
// Doesn't look like TypeScript has good support for creating an
2548-
// object containing dotted keys:
2549-
// https://stackoverflow.com/questions/58434389/typescript-deep-keyof-of-a-nested-object
2550-
| mongodb.QuerySelector<any>
2551-
| any;
2552-
} | any; // Because TS doesn't have good support for creating an object with dotted keys, including `.$.` re: #10075
2539+
type OnlyFieldsOfType<TSchema, FieldType = any, AssignableType = FieldType> = {
2540+
[key in keyof TSchema]?: [Extract<TSchema[key], FieldType>] extends [never] ? never : AssignableType;
2541+
};
25532542

25542543
/** @see https://docs.mongodb.com/manual/reference/operator/update */
25552544
type _UpdateQuery<TSchema> = {
25562545
/** @see https://docs.mongodb.com/manual/reference/operator/update-field/ */
2557-
$currentDate?: mongodb.OnlyFieldsOfType<TSchema, NativeDate | mongodb.Timestamp, true | { $type: 'date' | 'timestamp' }> | any;
2558-
$inc?: mongodb.OnlyFieldsOfType<TSchema, NumericTypes | undefined> | any;
2559-
$min?: mongodb.MatchKeysAndValues<TSchema>;
2560-
$max?: mongodb.MatchKeysAndValues<TSchema>;
2561-
$mul?: mongodb.OnlyFieldsOfType<TSchema, NumericTypes | undefined> | any;
2546+
$currentDate?: OnlyFieldsOfType<TSchema, NativeDate, true | { $type: 'date' | 'timestamp' }> & AnyObject;
2547+
$inc?: OnlyFieldsOfType<TSchema, NumericTypes | undefined> & AnyObject;
2548+
$min?: OnlyFieldsOfType<TSchema, any, any> & AnyObject;
2549+
$max?: OnlyFieldsOfType<TSchema, any, any> & AnyObject;
2550+
$mul?: OnlyFieldsOfType<TSchema, NumericTypes | undefined> & AnyObject;
25622551
$rename?: { [key: string]: string };
2563-
$set?: mongodb.MatchKeysAndValues<TSchema>;
2564-
$setOnInsert?: mongodb.MatchKeysAndValues<TSchema>;
2565-
$unset?: mongodb.OnlyFieldsOfType<TSchema, any, any> | any;
2552+
$set?: OnlyFieldsOfType<TSchema, any, any> & AnyObject;
2553+
$setOnInsert?: OnlyFieldsOfType<TSchema, any, any> & AnyObject;
2554+
$unset?: OnlyFieldsOfType<TSchema, any, any> & AnyObject;
25662555

25672556
/** @see https://docs.mongodb.com/manual/reference/operator/update-array/ */
2568-
$addToSet?: mongodb.SetFields<TSchema> | any;
2569-
$pop?: mongodb.OnlyFieldsOfType<TSchema, ReadonlyArray<any>, 1 | -1> | any;
2570-
$pull?: PullOperator<TSchema>;
2571-
$push?: mongodb.PushOperator<TSchema> | any;
2572-
$pullAll?: mongodb.PullAllOperator<TSchema> | any;
2557+
$addToSet?: OnlyFieldsOfType<TSchema, any[], any> & AnyObject;
2558+
$pop?: OnlyFieldsOfType<TSchema, ReadonlyArray<any>, 1 | -1> & AnyObject;
2559+
$pull?: OnlyFieldsOfType<TSchema, ReadonlyArray<any>, any> & AnyObject;
2560+
$push?: OnlyFieldsOfType<TSchema, ReadonlyArray<any>, any> & AnyObject;
2561+
$pullAll?: OnlyFieldsOfType<TSchema, ReadonlyArray<any>, any> & AnyObject;
25732562

25742563
/** @see https://docs.mongodb.com/manual/reference/operator/update-bitwise/ */
25752564
$bit?: {
@@ -2585,7 +2574,18 @@ declare module 'mongoose' {
25852574
{ $replaceRoot: any } |
25862575
{ $replaceWith: any };
25872576

2588-
export type UpdateQuery<T> = (_UpdateQuery<DocumentDefinition<T>> & mongodb.MatchKeysAndValues<DocumentDefinition<T>>);
2577+
type __UpdateDefProperty<T> =
2578+
0 extends (1 & T) ? T : // any
2579+
T extends unknown[] ? LeanArray<T> : // Array
2580+
T extends Document ? LeanDocument<T> : // Subdocument
2581+
[Extract<T, mongodb.ObjectId>] extends [never] ? T :
2582+
T | string;
2583+
type __UpdateQueryDef<T> = {
2584+
[K in keyof T]: __UpdateDefProperty<T[K]>;
2585+
};
2586+
type _UpdateQueryDef<T> = __UpdateQueryDef<T>;
2587+
2588+
export type UpdateQuery<T> = (_UpdateQuery<_UpdateQueryDef<T>> & mongodb.MatchKeysAndValues<_UpdateQueryDef<LeanDocument<T>>>);
25892589

25902590
type _AllowStringsForIds<T> = {
25912591
[K in keyof T]: [Extract<T[K], mongodb.ObjectId>] extends [never]

0 commit comments

Comments
 (0)
Please sign in to comment.