Skip to content

Commit 161db22

Browse files
committed
fix: address PR feedback
1 parent c1c9a1e commit 161db22

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
@@ -119,11 +119,11 @@ collectionT.find({ 'meta.deep.nested.level': 123 });
119119
collectionT.find({ meta: { deep: { nested: { level: 123 } } } }); // no impact on actual nesting
120120
collectionT.find({ 'friends.0.name': 'John' });
121121
collectionT.find({ 'playmates.0.name': 'John' });
122+
// supports arrays with primitive types
123+
collectionT.find({ 'treats.0': 'bone' });
122124

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

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

143146
// Nested arrays aren't checked
144-
expectType<Filter<PetModel>>({ 'meta.deep.nestedArray.0': 'not a number' });
145-
expectNotType<Filter<PetModel>>({ 'meta.deep.nestedArray.23': 'not a number' });
147+
expectNotType<Filter<PetModel>>({ 'meta.deep.nestedArray.0': 'not a number' });
146148

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

0 commit comments

Comments
 (0)