Skip to content

Commit

Permalink
fix(NODE-3599): incorrect indexes return type (#2980)
Browse files Browse the repository at this point in the history
  • Loading branch information
dariakp committed Sep 13, 2021
1 parent 6d42267 commit 122b9f3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/collection.ts
Expand Up @@ -1190,14 +1190,14 @@ export class Collection<TSchema extends Document = Document> {
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
indexes(): Promise<Document>;
indexes(callback: Callback<Document>): void;
indexes(options: IndexInformationOptions): Promise<Document>;
indexes(options: IndexInformationOptions, callback: Callback<Document>): void;
indexes(): Promise<Document[]>;
indexes(callback: Callback<Document[]>): void;
indexes(options: IndexInformationOptions): Promise<Document[]>;
indexes(options: IndexInformationOptions, callback: Callback<Document[]>): void;
indexes(
options?: IndexInformationOptions | Callback<Document>,
callback?: Callback<Document>
): Promise<Document> | void {
options?: IndexInformationOptions | Callback<Document[]>,
callback?: Callback<Document[]>
): Promise<Document[]> | void {
if (typeof options === 'function') (callback = options), (options = {});

return executeOperation(
Expand Down
4 changes: 2 additions & 2 deletions src/operations/indexes.ts
Expand Up @@ -159,7 +159,7 @@ function makeIndexSpec(indexSpec: IndexSpecification, options: any): IndexDescri
}

/** @internal */
export class IndexesOperation extends AbstractOperation<Document> {
export class IndexesOperation extends AbstractOperation<Document[]> {
options: IndexInformationOptions;
collection: Collection;

Expand All @@ -169,7 +169,7 @@ export class IndexesOperation extends AbstractOperation<Document> {
this.collection = collection;
}

execute(server: Server, session: ClientSession, callback: Callback<Document>): void {
execute(server: Server, session: ClientSession, callback: Callback<Document[]>): void {
const coll = this.collection;
const options = this.options;

Expand Down
26 changes: 26 additions & 0 deletions 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<Promise<Document[]>>(collection.indexes());
expectType<Promise<Document[]>>(collection.indexes({}));

// Explicit check for iterable result
for (const index of await collection.indexes()) {
expectType<Document>(index);
}

// Callback variant testing
collection.indexes((err, indexes) => {
expectType<AnyError | undefined>(err);
expectType<Document[] | undefined>(indexes);
});

collection.indexes({}, (err, indexes) => {
expectType<AnyError | undefined>(err);
expectType<Document[] | undefined>(indexes);
});

0 comments on commit 122b9f3

Please sign in to comment.