diff --git a/test/types/queries.test.ts b/test/types/queries.test.ts index 7a881947f7c..7a6550f7abf 100644 --- a/test/types/queries.test.ts +++ b/test/types/queries.test.ts @@ -325,5 +325,19 @@ function gh11964() { /* ... */ } } +} +function gh12091() { + interface IUser{ + friendsNames: string[]; + } + const userSchema = new Schema({ + friendsNames: [String] + }); + + const update: UpdateQuery = { $addToSet: { friendsNames: 'John Doe' } }; + if (!update?.$addToSet) { + return; + } + update.$addToSet.friendsNames = 'Jane Doe'; } diff --git a/types/index.d.ts b/types/index.d.ts index 8b7a63bbc41..4396c60baa8 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -437,6 +437,10 @@ declare module 'mongoose' { export type SortOrder = -1 | 1 | 'asc' | 'ascending' | 'desc' | 'descending'; + type Mutable = { + -readonly [K in keyof T]: T[K]; + } + type _UpdateQuery = { /** @see https://docs.mongodb.com/manual/reference/operator/update-field/ */ $currentDate?: AnyKeys & AnyObject; @@ -450,10 +454,10 @@ declare module 'mongoose' { $unset?: AnyKeys & AnyObject; /** @see https://docs.mongodb.com/manual/reference/operator/update-array/ */ - $addToSet?: mongodb.SetFields; + $addToSet?: Mutable>; $pop?: AnyKeys & AnyObject; - $pull?: mongodb.PullOperator; - $push?: mongodb.PushOperator; + $pull?: Mutable>; + $push?: Mutable>; $pullAll?: mongodb.PullAllOperator; /** @see https://docs.mongodb.com/manual/reference/operator/update-bitwise/ */