Skip to content

Commit 3ec01fa

Browse files
committedDec 9, 2020
fix(index.d.ts): allow calling mongoose.model() and Connection#model() with model as generic param
Fix #9678
1 parent ccfa041 commit 3ec01fa

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed
 

‎index.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ declare module "mongoose" {
242242

243243
/** Defines or retrieves a model. */
244244
model<T extends Document>(name: string, schema?: Schema, collection?: string): Model<T>;
245+
model<T extends Document, U extends Model<T>>(
246+
name: string,
247+
schema?: Schema,
248+
collection?: string,
249+
skipInit?: boolean
250+
): U;
245251

246252
/** Returns an array of model names created on this connection. */
247253
modelNames(): Array<string>;

‎test/typescript/models.ts

+14
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,17 @@ const ExpiresSchema = new Schema({
2323
});
2424

2525
const aggregated: Promise<Document> = Test.aggregate([]).then(res => res[0]);
26+
27+
interface IProject extends Document {
28+
name: String;
29+
}
30+
31+
interface ProjectModel extends Model<IProject> {
32+
myStatic(): number;
33+
}
34+
35+
const projectSchema: Schema = new Schema({ name: String });
36+
projectSchema.statics.myStatic = () => 42;
37+
38+
const Project = connection.model<IProject, ProjectModel>('Project', projectSchema);
39+
Project.myStatic();

0 commit comments

Comments
 (0)
Please sign in to comment.