diff --git a/types/mongodb/index.d.ts b/types/mongodb/index.d.ts index 3048da4dd4..efdf490291 100644 --- a/types/mongodb/index.d.ts +++ b/types/mongodb/index.d.ts @@ -487,8 +487,9 @@ export interface Collection { bulkWrite(operations: Object[], options?: CollectionBluckWriteOptions): Promise; bulkWrite(operations: Object[], options: CollectionBluckWriteOptions, callback: MongoCallback): void; /** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#count */ + count(callback: MongoCallback): void; count(query: Object, callback: MongoCallback): void; - count(query: Object, options?: MongoCountPreferences): Promise; + count(query?: Object, options?: MongoCountPreferences): Promise; count(query: Object, options: MongoCountPreferences, callback: MongoCallback): void; /** http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#createIndex */ createIndex(fieldOrSpec: string | any, callback: MongoCallback): void; diff --git a/types/mongodb/mongodb-tests.ts b/types/mongodb/mongodb-tests.ts index bdbddb1250..a5a27bd671 100644 --- a/types/mongodb/mongodb-tests.ts +++ b/types/mongodb/mongodb-tests.ts @@ -35,7 +35,29 @@ MongoClient.connect('mongodb://127.0.0.1:27017/test', options, function (err: mo var collection = db.collection('test_insert'); collection.insertOne({ a: 2 }, function (err: mongodb.MongoError, docs: any) { - collection.count(function (err: mongodb.MongoError, count: any) { + // Intentionally omitted type annotation from 'count'. + // This way it requires a more accurate typedef which allows inferring that it's a number. + collection.count(function (err: mongodb.MongoError, count) { + console.log(format("count = %s", count)); + }); + + collection.count().then(function (count: number) { + console.log(format("count = %s", count)); + }); + + collection.count({ foo: 1 }, function (err: mongodb.MongoError, count: number) { + console.log(format("count = %s", count)); + }); + + collection.count({ foo: 1 }).then(function (count: number) { + console.log(format("count = %s", count)); + }); + + collection.count({ foo: 1 }, { limit: 10 }, function (err: mongodb.MongoError, count: number) { + console.log(format("count = %s", count)); + }); + + collection.count({ foo: 1 }, { limit: 10 }).then(function (count: number) { console.log(format("count = %s", count)); });