Skip to content

Commit 691ef3d

Browse files
committed
fix: address PR feedback
1 parent 286882f commit 691ef3d

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

src/mongo_types.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,13 @@ export type PropertyType<Type, Property extends string> = string extends Propert
441441
? unknown
442442
: Property extends keyof Type
443443
? Type[Property]
444+
: Property extends `${number}`
445+
? Type extends ReadonlyArray<infer ArrayType>
446+
? ArrayType
447+
: unknown
444448
: Property extends `${infer Key}.${infer Rest}`
445449
? Key extends `${number}`
446-
? Type extends Array<infer ArrayType>
447-
? PropertyType<ArrayType, Rest>
448-
: Type extends ReadonlyArray<infer ArrayType>
450+
? Type extends ReadonlyArray<infer ArrayType>
449451
? PropertyType<ArrayType, Rest>
450452
: unknown
451453
: Key extends keyof Type
@@ -455,10 +457,20 @@ export type PropertyType<Type, Property extends string> = string extends Propert
455457

456458
// We dont't support nested circular references
457459
/** @public */
458-
export type NestedPaths<Type> = Type extends string | number | boolean | Date | ObjectId
460+
export type NestedPaths<Type> = Type extends
461+
| string
462+
| number
463+
| boolean
464+
| BSONRegExp
465+
| Binary
466+
| Date
467+
| Decimal128
468+
| Double
469+
| Int32
470+
| Long
471+
| ObjectId
472+
| Timestamp
459473
? []
460-
: Type extends Array<infer ArrayType>
461-
? [number, ...NestedPaths<ArrayType>]
462474
: Type extends ReadonlyArray<infer ArrayType>
463475
? [number, ...NestedPaths<ArrayType>]
464476
: // eslint-disable-next-line @typescript-eslint/ban-types

test/types/community/collection/filterQuery.test-d.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ collectionT.find({ 'meta.deep.nested.level': 123 });
115115
collectionT.find({ meta: { deep: { nested: { level: 123 } } } }); // no impact on actual nesting
116116
collectionT.find({ 'friends.0.name': 'John' });
117117
collectionT.find({ 'playmates.0.name': 'John' });
118+
// supports arrays with primitive types
119+
collectionT.find({ 'treats.0': 'bone' });
118120

119-
// There's an issue with the special BSON types
120-
collectionT.find({ 'numOfPats.__isLong__': true });
121+
// Handle special BSON types
121122
collectionT.find({ numOfPats: Long.fromBigInt(2n) });
122-
collectionT.find({ 'playTimePercent.bytes.BYTES_PER_ELEMENT': 1 });
123123
collectionT.find({ playTimePercent: new Decimal128('123.2') });
124124

125125
// works with some extreme indexes
@@ -135,10 +135,12 @@ expectNotType<Filter<PetModel>>({ 'meta.deep.nested.level': true });
135135
expectNotType<Filter<PetModel>>({ 'meta.deep.nested.level': new Date() });
136136
expectNotType<Filter<PetModel>>({ 'friends.0.name': 123 });
137137
expectNotType<Filter<PetModel>>({ 'playmates.0.name': 123 });
138+
expectNotType<Filter<PetModel>>({ 'treats.0': 123 });
139+
expectNotType<Filter<PetModel>>({ 'numOfPats.__isLong__': true });
140+
expectNotType<Filter<PetModel>>({ 'playTimePercent.bytes.BYTES_PER_ELEMENT': 1 });
138141

139142
// Nested arrays aren't checked
140-
expectType<Filter<PetModel>>({ 'meta.deep.nestedArray.0': 'not a number' });
141-
expectNotType<Filter<PetModel>>({ 'meta.deep.nestedArray.23': 'not a number' });
143+
expectNotType<Filter<PetModel>>({ 'meta.deep.nestedArray.0': 'not a number' });
142144

143145
/// it should query __array__ fields by exact match
144146
await collectionT.find({ treats: ['kibble', 'bone'] }).toArray();

0 commit comments

Comments
 (0)