@@ -6,14 +6,38 @@ import type { CollectionInfo, ListCollectionsCursor } from '../../src/operations
66const db = new MongoClient ( '' ) . db ( ) ;
77
88// We default to the CollectionInfo result type
9- expectType < ListCollectionsCursor < CollectionInfo > > ( db . listCollections ( ) ) ;
10- // We do not return the string result, and since its a runtime option there's not a great TS way to capture this
11- expectNotType < ListCollectionsCursor < string > > ( db . listCollections ( ) ) ;
9+ expectType < ListCollectionsCursor < Pick < CollectionInfo , 'name' | 'type' > | CollectionInfo > > (
10+ db . listCollections ( )
11+ ) ;
12+ // By default it isn't narrowed to either type
13+ expectNotType < ListCollectionsCursor < Pick < CollectionInfo , 'name' | 'type' > > > ( db . listCollections ( ) ) ;
14+ expectNotType < ListCollectionsCursor < CollectionInfo > > ( db . listCollections ( ) ) ;
15+
16+ // Testings each argument variation
17+ db . listCollections ( ) ;
18+ db . listCollections ( { a : 2 } ) ;
19+ db . listCollections ( { a : 2 } , { batchSize : 2 } ) ;
1220
13- // toArray is a good way for TS users to keep their code simple
1421const collections = await db . listCollections ( ) . toArray ( ) ;
15- expectType < CollectionInfo [ ] > ( collections ) ;
22+ expectType < ( CollectionInfo | string ) [ ] > ( collections ) ;
23+
24+ const nameOnly = await db . listCollections ( { } , { nameOnly : true } ) . toArray ( ) ;
25+ expectType < Pick < CollectionInfo , 'name' | 'type' > [ ] > ( nameOnly ) ;
26+
27+ const fullInfo = await db . listCollections ( { } , { nameOnly : false } ) . toArray ( ) ;
28+ expectType < CollectionInfo [ ] > ( fullInfo ) ;
29+
30+ const couldBeEither = await db . listCollections ( { } , { nameOnly : Math . random ( ) > 0.5 } ) . toArray ( ) ;
31+ expectType < ( CollectionInfo | string ) [ ] > ( couldBeEither ) ;
1632
17- // toArray takes an override so here we can get an array of strings easily
18- const collectionNames = await db . listCollections ( { } , { nameOnly : true } ) . toArray < string > ( ) ;
19- expectType < string [ ] > ( collectionNames ) ;
33+ // Showing here that:
34+ // regardless of the option the generic parameter can be used to coerce the result if need be
35+ // note the nameOnly: false, yet strings are returned
36+ const overridden = await db
37+ . listCollections < Pick < CollectionInfo , 'name' | 'type' > > ( { } , { nameOnly : false } )
38+ . toArray ( ) ;
39+ expectType < Pick < CollectionInfo , 'name' | 'type' > [ ] > ( overridden ) ;
40+ const overriddenWithToArray = await db
41+ . listCollections ( { } , { nameOnly : false } )
42+ . toArray < Pick < CollectionInfo , 'name' | 'type' > > ( ) ;
43+ expectType < Pick < CollectionInfo , 'name' | 'type' > [ ] > ( overriddenWithToArray ) ;
0 commit comments