Skip to content

Commit

Permalink
fix(NODE-3681): Typescript error in Collection.findOneAndModify Updat…
Browse files Browse the repository at this point in the history
…eFilter $currentDate (#4047)
  • Loading branch information
aditi-khare-mongoDB committed Apr 3, 2024
1 parent 0b85f39 commit 2aa33a5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/mongo_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export type NotAcceptedFields<TSchema, FieldType> = {
/** @public */
export type OnlyFieldsOfType<TSchema, FieldType = any, AssignableType = FieldType> = IsAny<
TSchema[keyof TSchema],
Record<string, FieldType>,
AssignableType extends FieldType ? Record<string, FieldType> : Record<string, AssignableType>,
AcceptedFields<TSchema, FieldType, AssignableType> &
NotAcceptedFields<TSchema, FieldType> &
Record<string, AssignableType>
Expand Down
7 changes: 7 additions & 0 deletions test/types/community/collection/updateX.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ expectAssignable<UpdateFilter<Document>>({ $inc: { anyKeyWhatsoever: 2 } });
// But this no longer asserts anything about what the original keys map to
expectNotType<UpdateFilter<Document>>({ $inc: { numberField: '2' } });

expectAssignable<UpdateFilter<Document>>({
$currentDate: { anyKeyWhatsoever: { $type: 'timestamp' } }
});
expectAssignable<UpdateFilter<Document>>({ $currentDate: { anyKeyWhatsoever: { $type: 'date' } } });
expectAssignable<UpdateFilter<Document>>({ $currentDate: { anyKeyWhatsoever: true } });
expectNotType<UpdateFilter<Document>>({ $currentDate: { anyKeyWhatsoever: 'true' } });
expectNotType<UpdateFilter<Document>>({ $currentDate: { anyKeyWhatsoever: { key2: true } } });
// collection.updateX tests
const client = new MongoClient('');
const db = client.db('test');
Expand Down
3 changes: 3 additions & 0 deletions test/types/helper_types.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ expectAssignable<OnlyFieldsOfType<{ a: number; b: string }, number>>({ a: 2 });
expectAssignable<OnlyFieldsOfType<{ a: number; b: string }, string>>({ b: 'hello' });
expectAssignable<OnlyFieldsOfType<{ a: number; b: string }, string, boolean>>({ b: true });

// test the case in which AssignableType does not inherit from FieldType, and AssignableType is provided
expectAssignable<OnlyFieldsOfType<any, string, boolean>>({ b: false });

// test generic schema, essentially we expect nearly no safety here
expectAssignable<OnlyFieldsOfType<Document, NumericType | undefined>>({ someKey: 2 });
// We can still at least enforce the type that the keys map to
Expand Down

0 comments on commit 2aa33a5

Please sign in to comment.