1
1
import { expectAssignable , expectNotType , expectType } from 'tsd' ;
2
- import { FindCursor , FindOptions , MongoClient , Document , Collection , Db } from '../../../../src' ;
2
+ import {
3
+ FindCursor ,
4
+ FindOptions ,
5
+ MongoClient ,
6
+ Document ,
7
+ Collection ,
8
+ Db ,
9
+ WithId ,
10
+ ObjectId
11
+ } from '../../../../src' ;
3
12
import type { Projection , ProjectionOperators } from '../../../../src' ;
4
13
import type { PropExists } from '../../utility_types' ;
5
14
@@ -10,7 +19,11 @@ const collection = db.collection('test.find');
10
19
11
20
// Locate all the entries using find
12
21
collection . find ( { } ) . toArray ( ( _err , fields ) => {
13
- expectType < Document [ ] | undefined > ( fields ) ;
22
+ expectType < WithId < Document > [ ] | undefined > ( fields ) ;
23
+ if ( fields ) {
24
+ expectType < ObjectId > ( fields [ 0 ] . _id ) ;
25
+ expectNotType < ObjectId | undefined > ( fields [ 0 ] . _id ) ;
26
+ }
14
27
} ) ;
15
28
16
29
// test with collection type
@@ -26,7 +39,7 @@ collectionT.find({
26
39
$and : [ { numberField : { $gt : 0 } } , { numberField : { $lt : 100 } } ] ,
27
40
readonlyFruitTags : { $all : [ 'apple' , 'pear' ] }
28
41
} ) ;
29
- expectType < FindCursor < TestModel > > ( collectionT . find ( { } ) ) ;
42
+ expectType < FindCursor < WithId < TestModel > > > ( collectionT . find ( { } ) ) ;
30
43
31
44
await collectionT . findOne (
32
45
{ } ,
@@ -72,22 +85,24 @@ interface Bag {
72
85
73
86
const collectionBag = db . collection < Bag > ( 'bag' ) ;
74
87
75
- const cursor : FindCursor < Bag > = collectionBag . find ( { color : 'black' } ) ;
88
+ const cursor : FindCursor < WithId < Bag > > = collectionBag . find ( { color : 'black' } ) ;
76
89
77
90
cursor . toArray ( ( _err , bags ) => {
78
- expectType < Bag [ ] | undefined > ( bags ) ;
91
+ expectType < WithId < Bag > [ ] | undefined > ( bags ) ;
79
92
} ) ;
80
93
81
94
cursor . forEach (
82
95
bag => {
83
- expectType < Bag > ( bag ) ;
96
+ expectType < WithId < Bag > > ( bag ) ;
84
97
} ,
85
98
( ) => {
86
99
return null ;
87
100
}
88
101
) ;
89
102
90
- expectType < Bag | null > ( await collectionBag . findOne ( { color : 'red' } , { projection : { cost : 1 } } ) ) ;
103
+ expectType < WithId < Bag > | null > (
104
+ await collectionBag . findOne ( { color : 'red' } , { projection : { cost : 1 } } )
105
+ ) ;
91
106
92
107
const overrideFind = await collectionBag . findOne < { cost : number } > (
93
108
{ color : 'white' } ,
@@ -150,40 +165,48 @@ const colorsFreeze: ReadonlyArray<string> = Object.freeze(['blue', 'red']);
150
165
const colorsWritable : Array < string > = [ 'blue' , 'red' ] ;
151
166
152
167
// Permitted Readonly fields
153
- expectType < FindCursor < { color : string } > > ( colorCollection . find ( { color : { $in : colorsFreeze } } ) ) ;
154
- expectType < FindCursor < { color : string } > > ( colorCollection . find ( { color : { $in : colorsWritable } } ) ) ;
155
- expectType < FindCursor < { color : string } > > ( colorCollection . find ( { color : { $nin : colorsFreeze } } ) ) ;
156
- expectType < FindCursor < { color : string } > > (
168
+ expectType < FindCursor < WithId < { color : string } > > > (
169
+ colorCollection . find ( { color : { $in : colorsFreeze } } )
170
+ ) ;
171
+ expectType < FindCursor < WithId < { color : string } > > > (
172
+ colorCollection . find ( { color : { $in : colorsWritable } } )
173
+ ) ;
174
+ expectType < FindCursor < WithId < { color : string } > > > (
175
+ colorCollection . find ( { color : { $nin : colorsFreeze } } )
176
+ ) ;
177
+ expectType < FindCursor < WithId < { color : string } > > > (
157
178
colorCollection . find ( { color : { $nin : colorsWritable } } )
158
179
) ;
159
180
// $all and $elemMatch works against single fields (it's just redundant)
160
- expectType < FindCursor < { color : string } > > ( colorCollection . find ( { color : { $all : colorsFreeze } } ) ) ;
161
- expectType < FindCursor < { color : string } > > (
181
+ expectType < FindCursor < WithId < { color : string } > > > (
182
+ colorCollection . find ( { color : { $all : colorsFreeze } } )
183
+ ) ;
184
+ expectType < FindCursor < WithId < { color : string } > > > (
162
185
colorCollection . find ( { color : { $all : colorsWritable } } )
163
186
) ;
164
- expectType < FindCursor < { color : string } > > (
187
+ expectType < FindCursor < WithId < { color : string } > > > (
165
188
colorCollection . find ( { color : { $elemMatch : colorsFreeze } } )
166
189
) ;
167
- expectType < FindCursor < { color : string } > > (
190
+ expectType < FindCursor < WithId < { color : string } > > > (
168
191
colorCollection . find ( { color : { $elemMatch : colorsWritable } } )
169
192
) ;
170
193
171
194
const countCollection = client . db ( 'test_db' ) . collection < { count : number } > ( 'test_collection' ) ;
172
- expectType < FindCursor < { count : number } > > (
195
+ expectType < FindCursor < WithId < { count : number } > > > (
173
196
countCollection . find ( { count : { $bitsAnySet : Object . freeze ( [ 1 , 0 , 1 ] ) } } )
174
197
) ;
175
- expectType < FindCursor < { count : number } > > (
198
+ expectType < FindCursor < WithId < { count : number } > > > (
176
199
countCollection . find ( { count : { $bitsAnySet : [ 1 , 0 , 1 ] as number [ ] } } )
177
200
) ;
178
201
179
202
const listsCollection = client . db ( 'test_db' ) . collection < { lists : string [ ] } > ( 'test_collection' ) ;
180
203
await listsCollection . updateOne ( { } , { list : { $pullAll : Object . freeze ( [ 'one' , 'two' ] ) } } ) ;
181
- expectType < FindCursor < { lists : string [ ] } > > ( listsCollection . find ( { lists : { $size : 1 } } ) ) ;
204
+ expectType < FindCursor < WithId < { lists : string [ ] } > > > ( listsCollection . find ( { lists : { $size : 1 } } ) ) ;
182
205
183
206
const rdOnlyListsCollection = client
184
207
. db ( 'test_db' )
185
208
. collection < { lists : ReadonlyArray < string > } > ( 'test_collection' ) ;
186
- expectType < FindCursor < { lists : ReadonlyArray < string > } > > (
209
+ expectType < FindCursor < WithId < { lists : ReadonlyArray < string > } > > > (
187
210
rdOnlyListsCollection . find ( { lists : { $size : 1 } } )
188
211
) ;
189
212
@@ -196,7 +219,9 @@ expectNotType<FindCursor<{ color: string | { $in: ReadonlyArray<string> } }>>(
196
219
expectNotType < FindCursor < { color : { $in : number } } > > (
197
220
colorCollection . find ( { color : { $in : 3 as any } } ) // `as any` is to let us make this mistake and still show the result type isn't broken
198
221
) ;
199
- expectType < FindCursor < { color : string } > > ( colorCollection . find ( { color : { $in : 3 as any } } ) ) ;
222
+ expectType < FindCursor < WithId < { color : string } > > > (
223
+ colorCollection . find ( { color : { $in : 3 as any } } )
224
+ ) ;
200
225
201
226
// When you use the override, $in doesn't permit readonly
202
227
colorCollection . find < { color : string } > ( { color : { $in : colorsFreeze } } ) ;
@@ -228,12 +253,40 @@ interface TypedDb extends Db {
228
253
const typedDb = client . db ( 'test2' ) as TypedDb ;
229
254
230
255
const person = typedDb . collection ( 'people' ) . findOne ( { } ) ;
231
- expectType < Promise < Person | null > > ( person ) ;
256
+ expectType < Promise < WithId < Person > | null > > ( person ) ;
232
257
233
258
typedDb . collection ( 'people' ) . findOne ( { } , function ( _err , person ) {
234
- expectType < Person | null | undefined > ( person ) ; // null is if nothing is found, undefined is when there is an error defined
259
+ expectType < WithId < Person > | null | undefined > ( person ) ; // null is if nothing is found, undefined is when there is an error defined
235
260
} ) ;
236
261
237
262
typedDb . collection ( 'things' ) . findOne ( { } , function ( _err , thing ) {
238
- expectType < Thing | null | undefined > ( thing ) ;
263
+ expectType < WithId < Thing > | null | undefined > ( thing ) ;
239
264
} ) ;
265
+
266
+ interface SchemaWithTypicalId {
267
+ _id : ObjectId ;
268
+ name : string ;
269
+ }
270
+ const schemaWithTypicalIdCol = db . collection < SchemaWithTypicalId > ( 'a' ) ;
271
+ expectType < WithId < SchemaWithTypicalId > | null > ( await schemaWithTypicalIdCol . findOne ( ) ) ;
272
+ expectAssignable < SchemaWithTypicalId | null > ( await schemaWithTypicalIdCol . findOne ( ) ) ;
273
+
274
+ interface SchemaWithOptionalTypicalId {
275
+ _id ?: ObjectId ;
276
+ name : string ;
277
+ }
278
+ const schemaWithOptionalTypicalId = db . collection < SchemaWithOptionalTypicalId > ( 'a' ) ;
279
+ expectType < WithId < SchemaWithOptionalTypicalId > | null > ( await schemaWithOptionalTypicalId . findOne ( ) ) ;
280
+ expectAssignable < SchemaWithOptionalTypicalId | null > ( await schemaWithOptionalTypicalId . findOne ( ) ) ;
281
+
282
+ interface SchemaWithUserDefinedId {
283
+ _id : number ;
284
+ name : string ;
285
+ }
286
+ const schemaWithUserDefinedId = db . collection < SchemaWithUserDefinedId > ( 'a' ) ;
287
+ expectType < WithId < SchemaWithUserDefinedId > | null > ( await schemaWithUserDefinedId . findOne ( ) ) ;
288
+ const result = await schemaWithUserDefinedId . findOne ( ) ;
289
+ if ( result !== null ) {
290
+ expectType < number > ( result . _id ) ;
291
+ }
292
+ expectAssignable < SchemaWithUserDefinedId | null > ( await schemaWithUserDefinedId . findOne ( ) ) ;
0 commit comments