Skip to content

Commit

Permalink
Improve RawDocType returned by calling populate FN
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammad0-0ahmad committed Mar 26, 2022
1 parent 2cef381 commit 470ac4e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
25 changes: 25 additions & 0 deletions test/types/populate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IParent>('Parent', parentSchema);

const populateQuery = parent.findOne().populate<{ child: IChild }>('child');
const populateResult = await populateQuery;
const leanResult = await populateQuery.lean();

expectType<string>(populateResult.child.name);
expectType<string>(leanResult.child.name);
}
4 changes: 2 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1774,8 +1774,8 @@ declare module 'mongoose' {
polygon(path: string, ...coordinatePairs: number[][]): this;

/** Specifies paths which should be populated with other documents. */
populate<Paths = {}>(path: string | string[], select?: string | any, model?: string | Model<any, THelpers>, match?: any): QueryWithHelpers<UnpackedIntersection<ResultType, Paths>, DocType, THelpers, RawDocType>;
populate<Paths = {}>(options: PopulateOptions | (PopulateOptions | string)[]): QueryWithHelpers<UnpackedIntersection<ResultType, Paths>, DocType, THelpers, RawDocType>;
populate<Paths = {}, TRawDocType = UnpackedIntersection<ResultType, Paths>>(path: string | string[], select?: string | any, model?: string | Model<any, THelpers>, match?: any): QueryWithHelpers<TRawDocType, DocType, THelpers, TRawDocType>;
populate<Paths = {}, TRawDocType = UnpackedIntersection<ResultType, Paths>>(options: PopulateOptions | (PopulateOptions | string)[]): QueryWithHelpers<TRawDocType, DocType, THelpers, TRawDocType>;

/** Get/set the current projection (AKA fields). Pass `null` to remove the current projection. */
projection(): ProjectionFields<DocType> | null;
Expand Down

0 comments on commit 470ac4e

Please sign in to comment.