@@ -135,3 +135,58 @@ expectNotType<FindOptions<Car>>({
135135
136136printCar ( await car . findOne ( { } , options ) ) ;
137137printCar ( await car . findOne ( { } , optionsWithProjection ) ) ;
138+
139+ // Readonly tests -- NODE-3452
140+ const colorCollection = client . db ( 'test_db' ) . collection < { color : string } > ( 'test_collection' ) ;
141+ const colorsFreeze : ReadonlyArray < string > = Object . freeze ( [ 'blue' , 'red' ] ) ;
142+ const colorsWritable : Array < string > = [ 'blue' , 'red' ] ;
143+
144+ // Permitted Readonly fields
145+ expectType < FindCursor < { color : string } > > ( colorCollection . find ( { color : { $in : colorsFreeze } } ) ) ;
146+ expectType < FindCursor < { color : string } > > ( colorCollection . find ( { color : { $in : colorsWritable } } ) ) ;
147+ expectType < FindCursor < { color : string } > > ( colorCollection . find ( { color : { $nin : colorsFreeze } } ) ) ;
148+ expectType < FindCursor < { color : string } > > (
149+ colorCollection . find ( { color : { $nin : colorsWritable } } )
150+ ) ;
151+ // $all and $elemMatch works against single fields (it's just redundant)
152+ expectType < FindCursor < { color : string } > > ( colorCollection . find ( { color : { $all : colorsFreeze } } ) ) ;
153+ expectType < FindCursor < { color : string } > > (
154+ colorCollection . find ( { color : { $all : colorsWritable } } )
155+ ) ;
156+ expectType < FindCursor < { color : string } > > (
157+ colorCollection . find ( { color : { $elemMatch : colorsFreeze } } )
158+ ) ;
159+ expectType < FindCursor < { color : string } > > (
160+ colorCollection . find ( { color : { $elemMatch : colorsWritable } } )
161+ ) ;
162+
163+ const countCollection = client . db ( 'test_db' ) . collection < { count : number } > ( 'test_collection' ) ;
164+ expectType < FindCursor < { count : number } > > (
165+ countCollection . find ( { count : { $bitsAnySet : Object . freeze ( [ 1 , 0 , 1 ] ) } } )
166+ ) ;
167+ expectType < FindCursor < { count : number } > > (
168+ countCollection . find ( { count : { $bitsAnySet : [ 1 , 0 , 1 ] as number [ ] } } )
169+ ) ;
170+
171+ const listsCollection = client . db ( 'test_db' ) . collection < { lists : string [ ] } > ( 'test_collection' ) ;
172+ await listsCollection . updateOne ( { } , { list : { $pullAll : Object . freeze ( [ 'one' , 'two' ] ) } } ) ;
173+ expectType < FindCursor < { lists : string [ ] } > > ( listsCollection . find ( { lists : { $size : 1 } } ) ) ;
174+
175+ const rdOnlyListsCollection = client
176+ . db ( 'test_db' )
177+ . collection < { lists : ReadonlyArray < string > } > ( 'test_collection' ) ;
178+ expectType < FindCursor < { lists : ReadonlyArray < string > } > > (
179+ rdOnlyListsCollection . find ( { lists : { $size : 1 } } )
180+ ) ;
181+
182+ // Before NODE-3452's fix we would get this strange result that included the filter shape joined with the actual schema
183+ expectNotType < FindCursor < { color : string | { $in : ReadonlyArray < string > } } > > (
184+ colorCollection . find ( { color : { $in : colorsFreeze } } )
185+ ) ;
186+
187+ // This is related to another bug that will be fixed in NODE-3454
188+ expectType < FindCursor < { color : { $in : number } } > > ( colorCollection . find ( { color : { $in : 3 } } ) ) ;
189+
190+ // When you use the override, $in doesn't permit readonly
191+ colorCollection . find < { color : string } > ( { color : { $in : colorsFreeze } } ) ;
192+ colorCollection . find < { color : string } > ( { color : { $in : [ 'regularArray' ] } } ) ;
0 commit comments