Skip to content

Commit

Permalink
Merge pull request #11649 from mohammad0-0ahmad-forks/#11598
Browse files Browse the repository at this point in the history
Refactor doc.populate FN type.
  • Loading branch information
vkarpov15 committed Apr 22, 2022
2 parents e0f50c0 + bfeecbd commit 4ab02f8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
20 changes: 18 additions & 2 deletions test/types/document.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { Schema, model, Model, Document, Error, Types } from 'mongoose';
import { Schema, model, Model, Document, Types } from 'mongoose';
import { expectError, expectType } from 'tsd';

const schema: Schema = new Schema({ name: { type: 'String', required: true }, address: new Schema({ city: { type: String, required: true } }) });
const Drink = model('Drink', new Schema({
name: String
}));

const schema: Schema = new Schema({
name: { type: 'String', required: true },
address: new Schema({ city: { type: String, required: true } }),
favoritDrink: {
type: Schema.Types.ObjectId,
ref: Drink
}
});

interface ITestBase {
name?: string;
Expand Down Expand Up @@ -164,4 +175,9 @@ function gh11435() {
ItemSchema.pre('validate', function preValidate() {
expectType<Model<unknown>>(this.$model('Item1'));
});
}

async function gh11598() {
const doc = await Test.findOne().orFail();
doc.populate('favoritDrink', undefined, model('temp', new Schema()));
}
6 changes: 3 additions & 3 deletions types/document.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ declare module 'mongoose' {
$parent(): Document | undefined;

/** Populates document references. */
populate<Paths = {}>(path: string | PopulateOptions | (string | PopulateOptions)[], callback: Callback<this & Paths>): void;
populate<Paths = {}>(path: string, names: string, callback: Callback<this & Paths>): void;
populate<Paths = {}>(path: string, names: string): Promise<this & Paths>;
populate<Paths = {}>(path: string | PopulateOptions | (string | PopulateOptions)[]): Promise<this & Paths>;
populate<Paths = {}>(path: string | PopulateOptions | (string | PopulateOptions)[], callback: Callback<this & Paths>): void;
populate<Paths = {}>(path: string, select?: string | AnyObject, model?: Model<unknown>, match?: AnyObject, options?: PopulateOptions): Promise<this & Paths>;
populate<Paths = {}>(path: string, select?: string | AnyObject, model?: Model<unknown>, match?: AnyObject, options?: PopulateOptions, callback?: Callback<this & Paths>): void;

/** Gets _id(s) used during population of the given `path`. If the path was not populated, returns `undefined`. */
populated(path: string): any;
Expand Down

0 comments on commit 4ab02f8

Please sign in to comment.