Skip to content

Commit

Permalink
fix(types): make $addToSet fields mutable to allow programatically …
Browse files Browse the repository at this point in the history
…constructing $addToSet

Fix #12091
  • Loading branch information
vkarpov15 committed Jul 17, 2022
1 parent 201071b commit 9524f89
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
14 changes: 14 additions & 0 deletions test/types/queries.test.ts
Expand Up @@ -325,5 +325,19 @@ function gh11964() {
/* ... */
}
}
}

function gh12091() {
interface IUser{
friendsNames: string[];
}
const userSchema = new Schema<IUser>({
friendsNames: [String]
});

const update: UpdateQuery<IUser> = { $addToSet: { friendsNames: 'John Doe' } };
if (!update?.$addToSet) {
return;
}
update.$addToSet.friendsNames = 'Jane Doe';
}
10 changes: 7 additions & 3 deletions types/index.d.ts
Expand Up @@ -437,6 +437,10 @@ declare module 'mongoose' {

export type SortOrder = -1 | 1 | 'asc' | 'ascending' | 'desc' | 'descending';

type Mutable<T> = {
-readonly [K in keyof T]: T[K];
}

type _UpdateQuery<TSchema> = {
/** @see https://docs.mongodb.com/manual/reference/operator/update-field/ */
$currentDate?: AnyKeys<TSchema> & AnyObject;
Expand All @@ -450,10 +454,10 @@ declare module 'mongoose' {
$unset?: AnyKeys<TSchema> & AnyObject;

/** @see https://docs.mongodb.com/manual/reference/operator/update-array/ */
$addToSet?: mongodb.SetFields<TSchema>;
$addToSet?: Mutable<mongodb.SetFields<TSchema>>;
$pop?: AnyKeys<TSchema> & AnyObject;
$pull?: mongodb.PullOperator<TSchema>;
$push?: mongodb.PushOperator<TSchema>;
$pull?: Mutable<mongodb.PullOperator<TSchema>>;
$push?: Mutable<mongodb.PushOperator<TSchema>>;
$pullAll?: mongodb.PullAllOperator<TSchema>;

/** @see https://docs.mongodb.com/manual/reference/operator/update-bitwise/ */
Expand Down

0 comments on commit 9524f89

Please sign in to comment.