Skip to content

Commit 8dfc4cc

Browse files
committed
fix(index.d.ts): unpack array when using generic type override with populate()
Fix #11027
1 parent 69b67ad commit 8dfc4cc

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,6 +2116,8 @@ declare module 'mongoose' {
21162116

21172117
type QueryWithHelpers<ResultType, DocType, THelpers = {}, RawDocType = DocType> = Query<ResultType, DocType, THelpers, RawDocType> & THelpers;
21182118

2119+
type UnpackedIntersection<T, U> = T extends (infer V)[] ? (V & U)[] : T & U;
2120+
21192121
class Query<ResultType, DocType, THelpers = {}, RawDocType = DocType> {
21202122
_mongooseOptions: MongooseQueryOptions;
21212123

@@ -2398,8 +2400,8 @@ declare module 'mongoose' {
23982400
polygon(path: string, ...coordinatePairs: number[][]): this;
23992401

24002402
/** Specifies paths which should be populated with other documents. */
2401-
populate<Paths = {}>(path: string | any, select?: string | any, model?: string | Model<any, THelpers>, match?: any): QueryWithHelpers<ResultType & Paths, DocType, THelpers, RawDocType>;
2402-
populate<Paths = {}>(options: PopulateOptions | Array<PopulateOptions>): QueryWithHelpers<ResultType & Paths, DocType, THelpers, RawDocType>;
2403+
populate<Paths = {}>(path: string | any, select?: string | any, model?: string | Model<any, THelpers>, match?: any): QueryWithHelpers<UnpackedIntersection<ResultType, Paths>, DocType, THelpers, RawDocType>;
2404+
populate<Paths = {}>(options: PopulateOptions | Array<PopulateOptions>): QueryWithHelpers<UnpackedIntersection<ResultType, Paths>, DocType, THelpers, RawDocType>;
24032405

24042406
/** Get/set the current projection (AKA fields). Pass `null` to remove the current projection. */
24052407
projection(fields?: any | null): any;

test/typescript/populate.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Schema, model, Document, PopulatedDoc } from 'mongoose';
1+
import { Schema, model, Document, PopulatedDoc, Types } from 'mongoose';
22
// Use the mongodb ObjectId to make instanceof calls possible
33
import { ObjectId } from 'mongodb';
44

@@ -91,4 +91,34 @@ async function testPathsParam() {
9191
return;
9292
}
9393
const name: string = story.author.name;
94+
}
95+
96+
function gh11014() {
97+
interface Parent {
98+
child?: Types.ObjectId
99+
name?: string
100+
}
101+
interface Child {
102+
name: string
103+
}
104+
interface PopulatedParent {
105+
child: Child | null
106+
}
107+
const ParentModel = model<Parent>(
108+
'Parent',
109+
new Schema({
110+
child: { type: 'ObjectId', ref: 'Child' },
111+
name: String
112+
})
113+
);
114+
const childSchema: Schema = new Schema({ name: String });
115+
const ChildModel = model<Child>('Child', childSchema);
116+
117+
// Populate with `Paths` generic `{ child: Child }` to override `child` path
118+
ParentModel.find({})
119+
.populate<{child: Child}>('child')
120+
.orFail()
121+
.then(parents => {
122+
parents.map(p => p.child.name);
123+
});
94124
}

0 commit comments

Comments
 (0)