From b5f7acb4474627d4d1834076222a6bc1c214a5cc Mon Sep 17 00:00:00 2001 From: Daria Pardue <81593090+dariakp@users.noreply.github.com> Date: Mon, 13 Sep 2021 13:03:29 -0400 Subject: [PATCH] fix(NODE-3599): incorrect indexes return type --- src/collection.ts | 14 +++++++------- src/operations/indexes.ts | 4 ++-- test/types/indexes_test-d.ts | 26 ++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 test/types/indexes_test-d.ts diff --git a/src/collection.ts b/src/collection.ts index 5d6aa98284..fd08502688 100644 --- a/src/collection.ts +++ b/src/collection.ts @@ -1190,14 +1190,14 @@ export class Collection { * @param options - Optional settings for the command * @param callback - An optional callback, a Promise will be returned if none is provided */ - indexes(): Promise; - indexes(callback: Callback): void; - indexes(options: IndexInformationOptions): Promise; - indexes(options: IndexInformationOptions, callback: Callback): void; + indexes(): Promise; + indexes(callback: Callback): void; + indexes(options: IndexInformationOptions): Promise; + indexes(options: IndexInformationOptions, callback: Callback): void; indexes( - options?: IndexInformationOptions | Callback, - callback?: Callback - ): Promise | void { + options?: IndexInformationOptions | Callback, + callback?: Callback + ): Promise | void { if (typeof options === 'function') (callback = options), (options = {}); return executeOperation( diff --git a/src/operations/indexes.ts b/src/operations/indexes.ts index 6f67452550..91b479f1b9 100644 --- a/src/operations/indexes.ts +++ b/src/operations/indexes.ts @@ -159,7 +159,7 @@ function makeIndexSpec(indexSpec: IndexSpecification, options: any): IndexDescri } /** @internal */ -export class IndexesOperation extends AbstractOperation { +export class IndexesOperation extends AbstractOperation { options: IndexInformationOptions; collection: Collection; @@ -169,7 +169,7 @@ export class IndexesOperation extends AbstractOperation { this.collection = collection; } - execute(server: Server, session: ClientSession, callback: Callback): void { + execute(server: Server, session: ClientSession, callback: Callback): void { const coll = this.collection; const options = this.options; diff --git a/test/types/indexes_test-d.ts b/test/types/indexes_test-d.ts new file mode 100644 index 0000000000..c0f8d1db34 --- /dev/null +++ b/test/types/indexes_test-d.ts @@ -0,0 +1,26 @@ +import { expectType } from 'tsd'; +import { MongoClient, Document, AnyError } from '../../src'; + +const client = new MongoClient(''); +const db = client.db('test'); +const collection = db.collection('test.find'); + +// Promise variant testing +expectType>(collection.indexes()); +expectType>(collection.indexes({})); + +// Explicit check for iterable result +for (const index of await collection.indexes()) { + expectType(index); +} + +// Callback variant testing +collection.indexes((err, indexes) => { + expectType(err); + expectType(indexes); +}); + +collection.indexes({}, (err, indexes) => { + expectType(err); + expectType(indexes); +});