-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
90 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); |
File renamed without changes.