-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
list_collections.test-d.ts
45 lines (35 loc) · 1.91 KB
/
list_collections.test-d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { expectType, expectNotType } from 'tsd';
import { MongoClient } from '../../src/mongo_client';
import type { CollectionInfo, ListCollectionsCursor } from '../../src/operations/list_collections';
const db = new MongoClient('').db();
type EitherCollectionInfoResult = CollectionInfo | Pick<CollectionInfo, 'name' | 'type'>;
// We default to the CollectionInfo result type
expectType<ListCollectionsCursor<Pick<CollectionInfo, 'name' | 'type'> | CollectionInfo>>(
db.listCollections()
);
// By default it isn't narrowed to either type
expectNotType<ListCollectionsCursor<Pick<CollectionInfo, 'name' | 'type'>>>(db.listCollections());
expectNotType<ListCollectionsCursor<CollectionInfo>>(db.listCollections());
// Testing each argument variation
db.listCollections();
db.listCollections({ a: 2 });
db.listCollections({ a: 2 }, { batchSize: 2 });
const collections = await db.listCollections().toArray();
expectType<EitherCollectionInfoResult[]>(collections);
const nameOnly = await db.listCollections({}, { nameOnly: true }).toArray();
expectType<Pick<CollectionInfo, 'name' | 'type'>[]>(nameOnly);
const fullInfo = await db.listCollections({}, { nameOnly: false }).toArray();
expectType<CollectionInfo[]>(fullInfo);
const couldBeEither = await db.listCollections({}, { nameOnly: Math.random() > 0.5 }).toArray();
expectType<EitherCollectionInfoResult[]>(couldBeEither);
// Showing here that:
// regardless of the option the generic parameter can be used to coerce the result if need be
// note the nameOnly: false, yet strings are returned
const overridden = await db
.listCollections<Pick<CollectionInfo, 'name' | 'type'>>({}, { nameOnly: false })
.toArray();
expectType<Pick<CollectionInfo, 'name' | 'type'>[]>(overridden);
const overriddenWithToArray = await db
.listCollections({}, { nameOnly: false })
.toArray<Pick<CollectionInfo, 'name' | 'type'>>();
expectType<Pick<CollectionInfo, 'name' | 'type'>[]>(overriddenWithToArray);