diff --git a/test/types/community/collection/findX.test-d.ts b/test/types/community/collection/findX.test-d.ts index 16c596807d2..9fb33a75f4b 100644 --- a/test/types/community/collection/findX.test-d.ts +++ b/test/types/community/collection/findX.test-d.ts @@ -1,5 +1,5 @@ import { expectAssignable, expectNotType, expectType } from 'tsd'; -import { FindCursor, FindOptions, MongoClient, Document } from '../../../../src'; +import { FindCursor, FindOptions, MongoClient, Document, Collection, Db } from '../../../../src'; import type { Projection, ProjectionOperators } from '../../../../src'; import type { PropExists } from '../../utility_types'; @@ -9,7 +9,7 @@ const db = client.db('test'); const collection = db.collection('test.find'); // Locate all the entries using find -collection.find({}).toArray((err, fields) => { +collection.find({}).toArray((_err, fields) => { expectType(fields); }); @@ -22,7 +22,7 @@ interface TestModel { } const collectionT = db.collection('testCollection'); -await collectionT.find({ +collectionT.find({ $and: [{ numberField: { $gt: 0 } }, { numberField: { $lt: 100 } }], readonlyFruitTags: { $all: ['apple', 'pear'] } }); @@ -74,7 +74,7 @@ const collectionBag = db.collection('bag'); const cursor: FindCursor = collectionBag.find({ color: 'black' }); -cursor.toArray((err, bags) => { +cursor.toArray((_err, bags) => { expectType(bags); }); @@ -198,3 +198,32 @@ expectType(findOptions); // This is just to check that we still export these type symbols expectAssignable({}); expectAssignable({}); + +// Ensure users can create a custom Db type that only contains specific +// collections (which are, in turn, strongly typed): +type Person = { + name: 'alice' | 'bob'; + age: number; +}; + +type Thing = { + location: 'shelf' | 'cupboard'; +}; + +interface TypedDb extends Db { + collection(name: 'people'): Collection; + collection(name: 'things'): Collection; +} + +const typedDb = client.db('test2') as TypedDb; + +const person = typedDb.collection('people').findOne({}); +expectType>(person); + +typedDb.collection('people').findOne({}, function (_err, person) { + expectType(person); +}); + +typedDb.collection('things').findOne({}, function (_err, thing) { + expectType(thing); +});