Skip to content

Commit 6786a3e

Browse files
committed
fix: address PR feedback
1 parent f68f71b commit 6786a3e

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

src/mongo_types.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,13 @@ export type PropertyType<Type, Property extends string> = string extends Propert
442442
? unknown
443443
: Property extends keyof Type
444444
? Type[Property]
445+
: Property extends `${number}`
446+
? Type extends ReadonlyArray<infer ArrayType>
447+
? ArrayType
448+
: unknown
445449
: Property extends `${infer Key}.${infer Rest}`
446450
? Key extends `${number}`
447-
? Type extends Array<infer ArrayType>
448-
? PropertyType<ArrayType, Rest>
449-
: Type extends ReadonlyArray<infer ArrayType>
451+
? Type extends ReadonlyArray<infer ArrayType>
450452
? PropertyType<ArrayType, Rest>
451453
: unknown
452454
: Key extends keyof Type
@@ -456,10 +458,20 @@ export type PropertyType<Type, Property extends string> = string extends Propert
456458

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

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ collectionT.find({ 'meta.deep.nested.level': 123 });
120120
collectionT.find({ meta: { deep: { nested: { level: 123 } } } }); // no impact on actual nesting
121121
collectionT.find({ 'friends.0.name': 'John' });
122122
collectionT.find({ 'playmates.0.name': 'John' });
123+
// supports arrays with primitive types
124+
collectionT.find({ 'treats.0': 'bone' });
123125

124-
// There's an issue with the special BSON types
125-
collectionT.find({ 'numOfPats.__isLong__': true });
126+
// Handle special BSON types
126127
collectionT.find({ numOfPats: Long.fromBigInt(2n) });
127-
collectionT.find({ 'playTimePercent.bytes.BYTES_PER_ELEMENT': 1 });
128128
collectionT.find({ playTimePercent: new Decimal128('123.2') });
129129

130130
// works with some extreme indexes
@@ -140,10 +140,12 @@ expectNotType<Filter<PetModel>>({ 'meta.deep.nested.level': true });
140140
expectNotType<Filter<PetModel>>({ 'meta.deep.nested.level': new Date() });
141141
expectNotType<Filter<PetModel>>({ 'friends.0.name': 123 });
142142
expectNotType<Filter<PetModel>>({ 'playmates.0.name': 123 });
143+
expectNotType<Filter<PetModel>>({ 'treats.0': 123 });
144+
expectNotType<Filter<PetModel>>({ 'numOfPats.__isLong__': true });
145+
expectNotType<Filter<PetModel>>({ 'playTimePercent.bytes.BYTES_PER_ELEMENT': 1 });
143146

144147
// Nested arrays aren't checked
145-
expectType<Filter<PetModel>>({ 'meta.deep.nestedArray.0': 'not a number' });
146-
expectNotType<Filter<PetModel>>({ 'meta.deep.nestedArray.23': 'not a number' });
148+
expectNotType<Filter<PetModel>>({ 'meta.deep.nestedArray.0': 'not a number' });
147149

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

0 commit comments

Comments
 (0)