diff --git a/types/mongoose/index.d.ts b/types/mongoose/index.d.ts index 2c58b764d3..cb20b294d2 100644 --- a/types/mongoose/index.d.ts +++ b/types/mongoose/index.d.ts @@ -2566,6 +2566,7 @@ declare module "mongoose" { * @param options.validateBeforeSave set to false to save without validating. * @param fn optional callback */ + save(options?: SaveOptions, fn?: (err: any, product: this, numAffected: number) => void): Promise; save(fn?: (err: any, product: this, numAffected: number) => void): Promise; /** @@ -2575,6 +2576,21 @@ declare module "mongoose" { __v?: number; } + interface SaveOptions { + safe?: boolean | WriteConcern; + validateBeforeSave?: boolean; + } + + interface WriteConcern { + j?: boolean; + w?: number | 'majority' | TagSet; + wtimeout?: number; + } + + interface TagSet { + [k: string]: string; + } + interface ModelProperties { /** Base Mongoose instance the model uses. */ base: typeof mongoose; diff --git a/types/mongoose/mongoose-tests.ts b/types/mongoose/mongoose-tests.ts index 4fb41ef3b2..7e3f815421 100644 --- a/types/mongoose/mongoose-tests.ts +++ b/types/mongoose/mongoose-tests.ts @@ -1234,8 +1234,20 @@ MongoModel.find({}).$where('indexOf("val") !== -1').exec(function (err, docs) { docs[0].__v; }); MongoModel.findById(999, function (err, doc) { + var handleSave = function(err: Error, product: mongoose.Document, numAffected: number) {}; doc.increment(); - doc.save(cb).then(cb).catch(cb); + doc.save(handleSave).then(cb).catch(cb); + doc.save({ validateBeforeSave: false }, handleSave).then(cb).catch(cb); + doc.save({ safe: true }, handleSave).then(cb).catch(cb); + doc.save({ safe: { w: 2, j: true } }, handleSave).then(cb).catch(cb); + doc.save({ safe: { w: 'majority', wtimeout: 10000 } }, handleSave).then(cb).catch(cb); + + // test if Typescript can infer the types of (err, product, numAffected) + doc.save(function(err, product, numAffected) { product.save(); }) + .then(function(p) { p.save() }).catch(cb); + doc.save({ validateBeforeSave: false }, function(err, product, numAffected) { + product.save(); + }).then(function(p) { p.save() }).catch(cb); }); MongoModel = (new MongoModel()).model('MongoModel'); var mongoModel = new MongoModel();