diff --git a/test/types/queries.test.ts b/test/types/queries.test.ts index 83c4eb9288..d259cb5d3d 100644 --- a/test/types/queries.test.ts +++ b/test/types/queries.test.ts @@ -187,6 +187,8 @@ expectError(Test.find({}, { 'docs.profiles': { name: 'aa' } })); // should suppo expectError(Test.find({}, { endDate: { toString: 1 } })); expectError(Test.find({}, { tags: { trim: 1 } })); expectError(Test.find({}, { child: { toJSON: 1 } })); +Test.find({}, { age: 1, _id: 0 }); +Test.find({}, { name: 0, age: 0, _id: 1 }); // Manual Casting using ProjectionType Test.find({}, { docs: { unknownParams: 1 } } as ProjectionType); diff --git a/types/index.d.ts b/types/index.d.ts index a62ab032cb..c50c7fbc85 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -684,10 +684,16 @@ declare module 'mongoose' { } : Element; type _IDType = { _id?: boolean | 1 | 0 }; - export type InclusionProjection = IsItRecordAndNotAny extends true ? Projector, true | 1> & _IDType : AnyObject; - export type ExclusionProjection = IsItRecordAndNotAny extends true ? Projector, false | 0> & _IDType : AnyObject; - - export type ProjectionType = (InclusionProjection & AnyObject) | (ExclusionProjection & AnyObject) | string; + export type InclusionProjection = IsItRecordAndNotAny extends true + ? Omit, true | 1>, '_id'> & _IDType + : AnyObject; + export type ExclusionProjection = IsItRecordAndNotAny extends true + ? Omit, false | 0>, '_id'> & _IDType + : AnyObject; + + export type ProjectionType = (InclusionProjection & AnyObject) + | (ExclusionProjection & AnyObject) + | string; export type SortValues = SortOrder; export type SortOrder = -1 | 1 | 'asc' | 'ascending' | 'desc' | 'descending';