Skip to content

Commit 122b9f3

Browse files
authored
fix(NODE-3599): incorrect indexes return type (#2980)
1 parent 6d42267 commit 122b9f3

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

src/collection.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1190,14 +1190,14 @@ export class Collection<TSchema extends Document = Document> {
11901190
* @param options - Optional settings for the command
11911191
* @param callback - An optional callback, a Promise will be returned if none is provided
11921192
*/
1193-
indexes(): Promise<Document>;
1194-
indexes(callback: Callback<Document>): void;
1195-
indexes(options: IndexInformationOptions): Promise<Document>;
1196-
indexes(options: IndexInformationOptions, callback: Callback<Document>): void;
1193+
indexes(): Promise<Document[]>;
1194+
indexes(callback: Callback<Document[]>): void;
1195+
indexes(options: IndexInformationOptions): Promise<Document[]>;
1196+
indexes(options: IndexInformationOptions, callback: Callback<Document[]>): void;
11971197
indexes(
1198-
options?: IndexInformationOptions | Callback<Document>,
1199-
callback?: Callback<Document>
1200-
): Promise<Document> | void {
1198+
options?: IndexInformationOptions | Callback<Document[]>,
1199+
callback?: Callback<Document[]>
1200+
): Promise<Document[]> | void {
12011201
if (typeof options === 'function') (callback = options), (options = {});
12021202

12031203
return executeOperation(

src/operations/indexes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ function makeIndexSpec(indexSpec: IndexSpecification, options: any): IndexDescri
159159
}
160160

161161
/** @internal */
162-
export class IndexesOperation extends AbstractOperation<Document> {
162+
export class IndexesOperation extends AbstractOperation<Document[]> {
163163
options: IndexInformationOptions;
164164
collection: Collection;
165165

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

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

test/types/indexes_test-d.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { expectType } from 'tsd';
2+
import { MongoClient, Document, AnyError } from '../../src';
3+
4+
const client = new MongoClient('');
5+
const db = client.db('test');
6+
const collection = db.collection('test.find');
7+
8+
// Promise variant testing
9+
expectType<Promise<Document[]>>(collection.indexes());
10+
expectType<Promise<Document[]>>(collection.indexes({}));
11+
12+
// Explicit check for iterable result
13+
for (const index of await collection.indexes()) {
14+
expectType<Document>(index);
15+
}
16+
17+
// Callback variant testing
18+
collection.indexes((err, indexes) => {
19+
expectType<AnyError | undefined>(err);
20+
expectType<Document[] | undefined>(indexes);
21+
});
22+
23+
collection.indexes({}, (err, indexes) => {
24+
expectType<AnyError | undefined>(err);
25+
expectType<Document[] | undefined>(indexes);
26+
});

0 commit comments

Comments
 (0)