Skip to content

Commit f0736a9

Browse files
authored
Merge pull request #15432 from Automattic/vkarpov15/gh-15431
types: correct handling of `_id` in ProjectionType
2 parents 4c7bdaa + 38f2da2 commit f0736a9

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

test/types/queries.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ expectError(Test.find({}, { 'docs.profiles': { name: 'aa' } })); // should suppo
187187
expectError(Test.find({}, { endDate: { toString: 1 } }));
188188
expectError(Test.find({}, { tags: { trim: 1 } }));
189189
expectError(Test.find({}, { child: { toJSON: 1 } }));
190+
Test.find({}, { age: 1, _id: 0 });
191+
Test.find({}, { name: 0, age: 0, _id: 1 });
190192

191193
// Manual Casting using ProjectionType
192194
Test.find({}, { docs: { unknownParams: 1 } } as ProjectionType<ITest>);

types/index.d.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -684,10 +684,16 @@ declare module 'mongoose' {
684684
}
685685
: Element;
686686
type _IDType = { _id?: boolean | 1 | 0 };
687-
export type InclusionProjection<T> = IsItRecordAndNotAny<T> extends true ? Projector<WithLevel1NestedPaths<T>, true | 1> & _IDType : AnyObject;
688-
export type ExclusionProjection<T> = IsItRecordAndNotAny<T> extends true ? Projector<WithLevel1NestedPaths<T>, false | 0> & _IDType : AnyObject;
689-
690-
export type ProjectionType<T> = (InclusionProjection<T> & AnyObject) | (ExclusionProjection<T> & AnyObject) | string;
687+
export type InclusionProjection<T> = IsItRecordAndNotAny<T> extends true
688+
? Omit<Projector<WithLevel1NestedPaths<T>, true | 1>, '_id'> & _IDType
689+
: AnyObject;
690+
export type ExclusionProjection<T> = IsItRecordAndNotAny<T> extends true
691+
? Omit<Projector<WithLevel1NestedPaths<T>, false | 0>, '_id'> & _IDType
692+
: AnyObject;
693+
694+
export type ProjectionType<T> = (InclusionProjection<T> & AnyObject)
695+
| (ExclusionProjection<T> & AnyObject)
696+
| string;
691697
export type SortValues = SortOrder;
692698

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

0 commit comments

Comments
 (0)