Skip to content

Commit

Permalink
fix(index.d.ts): allow calling mongoose.model() and `Connection#mod…
Browse files Browse the repository at this point in the history
…el()` with model as generic param

Fix #9678
  • Loading branch information
vkarpov15 committed Dec 9, 2020
1 parent ccfa041 commit 3ec01fa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.d.ts
Expand Up @@ -242,6 +242,12 @@ declare module "mongoose" {

/** Defines or retrieves a model. */
model<T extends Document>(name: string, schema?: Schema, collection?: string): Model<T>;
model<T extends Document, U extends Model<T>>(
name: string,
schema?: Schema,
collection?: string,
skipInit?: boolean
): U;

/** Returns an array of model names created on this connection. */
modelNames(): Array<string>;
Expand Down
14 changes: 14 additions & 0 deletions test/typescript/models.ts
Expand Up @@ -23,3 +23,17 @@ const ExpiresSchema = new Schema({
});

const aggregated: Promise<Document> = Test.aggregate([]).then(res => res[0]);

interface IProject extends Document {
name: String;
}

interface ProjectModel extends Model<IProject> {
myStatic(): number;
}

const projectSchema: Schema = new Schema({ name: String });
projectSchema.statics.myStatic = () => 42;

const Project = connection.model<IProject, ProjectModel>('Project', projectSchema);
Project.myStatic();

0 comments on commit 3ec01fa

Please sign in to comment.