From 4315ff5c4d1facf7c069bb227e1d779e6d3e33dc Mon Sep 17 00:00:00 2001 From: mohammad0-0ahmad Date: Wed, 23 Mar 2022 23:02:17 +0100 Subject: [PATCH 1/2] Improve RawDocType returned by calling populate FN --- test/types/populate.test.ts | 25 +++++++++++++++++++++++++ types/index.d.ts | 4 ++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/test/types/populate.test.ts b/test/types/populate.test.ts index 6933eff6f63..12336248857 100644 --- a/test/types/populate.test.ts +++ b/test/types/populate.test.ts @@ -197,4 +197,29 @@ function gh11544() { User.findOne({}).populate({ path: 'friends', strictPopulate: false }); User.findOne({}).populate({ path: 'friends', strictPopulate: true }); User.findOne({}).populate({ path: 'friends', populate: { path: 'someNestedPath', strictPopulate: false } }); +} + +async function _11532() { + interface IParent { + name: string; + child: Types.ObjectId; + } + interface IChild { + name: string; + } + + const parentSchema = new Schema( + { + name: { type: String, required: true }, + child: { type: Schema.Types.ObjectId, ref: 'Child', required: true } + }); + + const parent = model('Parent', parentSchema); + + const populateQuery = parent.findOne().populate<{ child: IChild }>('child'); + const populateResult = await populateQuery; + const leanResult = await populateQuery.lean(); + + expectType(populateResult.child.name); + expectType(leanResult.child.name); } \ No newline at end of file diff --git a/types/index.d.ts b/types/index.d.ts index 6a5d4db9cac..951a79f6759 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1774,8 +1774,8 @@ declare module 'mongoose' { polygon(path: string, ...coordinatePairs: number[][]): this; /** Specifies paths which should be populated with other documents. */ - populate(path: string | string[], select?: string | any, model?: string | Model, match?: any): QueryWithHelpers, DocType, THelpers, RawDocType>; - populate(options: PopulateOptions | (PopulateOptions | string)[]): QueryWithHelpers, DocType, THelpers, RawDocType>; + populate>(path: string | string[], select?: string | any, model?: string | Model, match?: any): QueryWithHelpers; + populate>(options: PopulateOptions | (PopulateOptions | string)[]): QueryWithHelpers; /** Get/set the current projection (AKA fields). Pass `null` to remove the current projection. */ projection(): ProjectionFields | null; From f28627369aa9a6a96ebe3ee5b3000db2e9a5e9b4 Mon Sep 17 00:00:00 2001 From: mohammad0-0ahmad Date: Sun, 27 Mar 2022 14:35:01 +0200 Subject: [PATCH 2/2] Refactor UnpackedIntersection type & some tests related to populate FN --- test/types/populate.test.ts | 9 +++++++-- types/index.d.ts | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/test/types/populate.test.ts b/test/types/populate.test.ts index 12336248857..748434ff8e0 100644 --- a/test/types/populate.test.ts +++ b/test/types/populate.test.ts @@ -170,12 +170,14 @@ function gh11503() { const User = model('friends', userSchema); User.findOne({}).populate('friends').then(user => { - expectType(user?.friends[0]); + if (!user) return; + expectType(user?.friends[0]); expectError(user?.friends[0].blocked); expectError(user?.friends.map(friend => friend.blocked)); }); - User.findOne({}).populate<{friends: Friend[]}>('friends').then(user => { + User.findOne({}).populate<{ friends: Friend[] }>('friends').then(user => { + if (!user) return; expectAssignable(user?.friends[0]); expectType(user?.friends[0].blocked); const firstFriendBlockedValue = user?.friends.map(friend => friend)[0]; @@ -220,6 +222,9 @@ async function _11532() { const populateResult = await populateQuery; const leanResult = await populateQuery.lean(); + if (!populateResult) return; expectType(populateResult.child.name); + + if (!leanResult) return; expectType(leanResult.child.name); } \ No newline at end of file diff --git a/types/index.d.ts b/types/index.d.ts index 951a79f6759..1a5acb6588c 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1469,7 +1469,7 @@ declare module 'mongoose' { type QueryWithHelpers = Query & THelpers; - type UnpackedIntersection = T extends (infer A)[] + type UnpackedIntersection = T extends null ? null : T extends (infer A)[] ? (Omit & U)[] : keyof U extends never ? T