From b6ffcfd93e7c383d7760ab9956e3acb3be2ddbdd Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 29 Apr 2024 15:59:13 -0400 Subject: [PATCH] test: lint and test cases for #14545 --- test/types/queries.test.ts | 35 +++++++++++++++++++++++++++++++++++ types/query.d.ts | 15 +++++++-------- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/test/types/queries.test.ts b/test/types/queries.test.ts index 297360b62d1..f10f26b3ad2 100644 --- a/test/types/queries.test.ts +++ b/test/types/queries.test.ts @@ -636,3 +636,38 @@ async function gh14525() { let doc2 = await ({} as ModelType).create({}); doc2 = await ({} as ModelType).findOne({}).populate('test').orFail().exec(); } + +async function gh14545() { + type Test = { + _id: Types.ObjectId; + + prop: string; + another: string; + + createdAt: number; + updatedAt: number; + }; + + const schema = new Schema({ + prop: { type: String }, + another: { type: String }, + createdAt: { type: Number }, + updatedAt: { type: Number } + }); + + type TestDocument = HydratedDocument; + type TestModel = Model; + + type SlimTest = Pick; + type SlimTestDocument = HydratedDocument; + + const M = model('Test', schema); + + const myDocs = await M.find({}).exec(); + const myDoc = await M.findOne({}).exec(); + + const myProjections = await M.find({}).select({ prop: 1 }).exec(); + expectType(myProjections); + const myProjection = await M.findOne({}).select({ prop: 1 }).exec(); + expectType(myProjection); +} diff --git a/types/query.d.ts b/types/query.d.ts index f4463e3f95b..3be8aea2259 100644 --- a/types/query.d.ts +++ b/types/query.d.ts @@ -697,14 +697,13 @@ declare module 'mongoose' { RawDocTypeOverride, {}, ResultType, - ResultType extends any[] ? - ResultType extends HydratedDocument[] ? - HydratedDocument[] : - RawDocTypeOverride[] : - | (ResultType extends HydratedDocument - ? HydratedDocument - : RawDocTypeOverride) - | (null extends ResultType ? null : never) + ResultType extends any[] + ? ResultType extends HydratedDocument[] + ? HydratedDocument[] + : RawDocTypeOverride[] + : (ResultType extends HydratedDocument + ? HydratedDocument + : RawDocTypeOverride) | (null extends ResultType ? null : never) >, DocType, THelpers,