Repository navigation
Description
The typings for mongoose are currently too 'loose', it doesn't enforce the model declaration when using .create, .update, etc. I could update the typings, but I think it might break current implementations because the parameters will have to obey the interface. (when using mongoose.model<IModelDefinedInterface>('DefinedInterface', DefinedInterfaceSchema)).
Also, the current state of the typings makes it hard without a typecast to access static methods on the model. ie:
declare module 'mongoose' {
export interface TheHackInterface<T extends Document> extends Model<T> {
}
}
/* ... */
implements IModelDefinedInterface extends mongoose.Document {
name: string;
ok?: boolean;
date?: Date;
}
export interface IModelDefinedStaticInterface extends mongoose.TheHackInterface<IModelDefinedInterface> {
getNewer(): mongoose.Query<IModelDefinedInterface>;
}
var Schema = new mongoose.Schema({
name: { type: String, required: true },
date: { type: Date },
ok: { type: Boolean, default: false }
});
Schema.statics.getNewer = function(){
return this.find({ date: { $gt: Date.now() } });
};
var
model = mongoose.model<IModelDefinedInterface>('DefinedInterface', Schema),
modelStatic = <IModelDefinedStaticInterface>model;
model.getNewer // can't
modelStatic.getNewer // ok
model.create({ lalala: true }) // ok when it shouldn't, missing required "name"Also some functions are wrongly defined. For example, when using create() with an object, the result is a created model, not an array of models. It's only an array of models if you create it using an array of objects.
Let me know if it's of any use (to update it to conform to version 4.x)
Activity
Updates to Mongoose 4.5.x (DefinitelyTyped#10084)