From b035a33d3ee1fd3d5bf1cc9db6b7f6b786810ec7 Mon Sep 17 00:00:00 2001 From: Eugene Korshenko Date: Mon, 10 Jan 2022 23:34:51 +0200 Subject: [PATCH 1/3] types: Fixes `Query.projection` method types --- index.d.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index a800870fcf6..523e29132af 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2117,6 +2117,8 @@ declare module 'mongoose' { type UnpackedIntersection = T extends (infer V)[] ? (V & U)[] : T & U; + type ProjectionFields = {[Key in keyof Omit, '__v'>]?: number} & Record; + class Query { _mongooseOptions: MongooseQueryOptions; @@ -2403,7 +2405,9 @@ declare module 'mongoose' { populate(options: PopulateOptions | Array): QueryWithHelpers, DocType, THelpers, RawDocType>; /** Get/set the current projection (AKA fields). Pass `null` to remove the current projection. */ - projection(fields?: any | null): this; + projection(): ProjectionFields | null; + projection(fields: null): null; + projection(fields?: ProjectionFields): ProjectionFields; /** Determines the MongoDB nodes from which to read. */ read(pref: string | mongodb.ReadPreferenceMode, tags?: any[]): this; From 35286d355c86e4bef8f74b7e9355bbd663501355 Mon Sep 17 00:00:00 2001 From: Eugene Korshenko Date: Tue, 11 Jan 2022 00:32:34 +0200 Subject: [PATCH 2/3] Add tests --- index.d.ts | 2 +- test/typescript/queries.ts | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 523e29132af..c84eb966175 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2407,7 +2407,7 @@ declare module 'mongoose' { /** Get/set the current projection (AKA fields). Pass `null` to remove the current projection. */ projection(): ProjectionFields | null; projection(fields: null): null; - projection(fields?: ProjectionFields): ProjectionFields; + projection(fields?: ProjectionFields | string): ProjectionFields; /** Determines the MongoDB nodes from which to read. */ read(pref: string | mongodb.ReadPreferenceMode, tags?: any[]): this; diff --git a/test/typescript/queries.ts b/test/typescript/queries.ts index f0dc5418e3e..baea0f4063a 100644 --- a/test/typescript/queries.ts +++ b/test/typescript/queries.ts @@ -116,6 +116,17 @@ query instanceof Query; Test.findOne().where({ name: 'test' }); Test.where().find({ name: 'test' }); +// Projection +const p0: Record = Test.find().projection({ + age: 1, + parent: 1, + 'docs.id': 1 +}); +const p1: Record = Test.find().projection('age docs.id'); +const p2: Record | null = Test.find().projection(); +const p3: null = Test.find().projection(null); + + // Super generic query function testGenericQuery(): void { interface CommonInterface extends Document { @@ -184,4 +195,4 @@ function gh10786() { if (true) { updateQuery.phone = 'XXXX'; } -} \ No newline at end of file +} From 63ba06b4ab4cc1b6c57dea9151b517f58d847626 Mon Sep 17 00:00:00 2001 From: Eugene Korshenko Date: Sun, 16 Jan 2022 16:19:18 +0200 Subject: [PATCH 3/3] types: Weakening of type strictness for ProjectionFields --- index.d.ts | 2 +- test/typescript/queries.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index c84eb966175..fc68b9b6b26 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2117,7 +2117,7 @@ declare module 'mongoose' { type UnpackedIntersection = T extends (infer V)[] ? (V & U)[] : T & U; - type ProjectionFields = {[Key in keyof Omit, '__v'>]?: number} & Record; + type ProjectionFields = {[Key in keyof Omit, '__v'>]?: any} & Record; class Query { _mongooseOptions: MongooseQueryOptions; diff --git a/test/typescript/queries.ts b/test/typescript/queries.ts index baea0f4063a..6983a84e0f2 100644 --- a/test/typescript/queries.ts +++ b/test/typescript/queries.ts @@ -118,7 +118,7 @@ Test.where().find({ name: 'test' }); // Projection const p0: Record = Test.find().projection({ - age: 1, + age: true, parent: 1, 'docs.id': 1 });