Skip to content

Commit

Permalink
Merge pull request #13117 from Automattic/IslandRhythms/paths-generic
Browse files Browse the repository at this point in the history
Add Path generic
  • Loading branch information
vkarpov15 authored Mar 2, 2023
2 parents 7d2c511 + 66d0235 commit 416784e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
30 changes: 30 additions & 0 deletions test/types/populate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,33 @@ function gh12136() {
}

}

async function gh13070() {
interface IParent {
name: string;
child: Types.ObjectId;
}
interface IChild {
name: string;
parent: Types.ObjectId;
}

const parentSchema = new Schema(
{
name: { type: String, required: true },
child: { type: Schema.Types.ObjectId, ref: 'Child', required: true }
});

const childSchema = new Schema(
{
name: { type: String, required: true },
parent: { type: Schema.Types.ObjectId, ref: 'Parent', required: true }
});

const Parent = model<IParent>('Parent', parentSchema);
const Child = model<IChild>('Child', childSchema);

const doc = await Parent.findOne().orFail();
const doc2 = await Child.populate<{ child: IChild }>(doc, 'child');
const name: string = doc2.child.name;
}
21 changes: 16 additions & 5 deletions types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,22 @@ declare module 'mongoose' {
modelName: string;

/** Populates document references. */
populate(docs: Array<any>, options: PopulateOptions | Array<PopulateOptions> | string,
callback?: Callback<(HydratedDocument<T, TMethodsAndOverrides, TVirtuals>)[]>): Promise<Array<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>>;
populate(doc: any, options: PopulateOptions | Array<PopulateOptions> | string,
callback?: Callback<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>;

populate(
docs: Array<any>,
options: PopulateOptions | Array<PopulateOptions> | string
): Promise<Array<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>>;
populate(
doc: any,
options: PopulateOptions | Array<PopulateOptions> | string,
): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>;
populate<Paths>(
docs: Array<any>,
options: PopulateOptions | Array<PopulateOptions> | string
): Promise<MergeType<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, Paths>[]>;
populate<Paths>(
doc: AnyObject,
options: PopulateOptions | Array<PopulateOptions> | string
): Promise<MergeType<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, Paths>>;

/** Casts and validates the given object against this model's schema, passing the given `context` to custom validators. */
validate(callback?: CallbackWithoutResult): Promise<void>;
Expand Down

0 comments on commit 416784e

Please sign in to comment.