Skip to content

Commit a8670a7

Browse files
fix(NODE-3681): Typescript error in Collection.findOneAndModify UpdateFilter $currentDate (#4047)
1 parent 13bf3c9 commit a8670a7

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

src/mongo_types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export type NotAcceptedFields<TSchema, FieldType> = {
219219
/** @public */
220220
export type OnlyFieldsOfType<TSchema, FieldType = any, AssignableType = FieldType> = IsAny<
221221
TSchema[keyof TSchema],
222-
Record<string, FieldType>,
222+
AssignableType extends FieldType ? Record<string, FieldType> : Record<string, AssignableType>,
223223
AcceptedFields<TSchema, FieldType, AssignableType> &
224224
NotAcceptedFields<TSchema, FieldType> &
225225
Record<string, AssignableType>

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

+7
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ expectAssignable<UpdateFilter<Document>>({ $inc: { anyKeyWhatsoever: 2 } });
6060
// But this no longer asserts anything about what the original keys map to
6161
expectNotType<UpdateFilter<Document>>({ $inc: { numberField: '2' } });
6262

63+
expectAssignable<UpdateFilter<Document>>({
64+
$currentDate: { anyKeyWhatsoever: { $type: 'timestamp' } }
65+
});
66+
expectAssignable<UpdateFilter<Document>>({ $currentDate: { anyKeyWhatsoever: { $type: 'date' } } });
67+
expectAssignable<UpdateFilter<Document>>({ $currentDate: { anyKeyWhatsoever: true } });
68+
expectNotType<UpdateFilter<Document>>({ $currentDate: { anyKeyWhatsoever: 'true' } });
69+
expectNotType<UpdateFilter<Document>>({ $currentDate: { anyKeyWhatsoever: { key2: true } } });
6370
// collection.updateX tests
6471
const client = new MongoClient('');
6572
const db = client.db('test');

test/types/helper_types.test-d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ expectAssignable<OnlyFieldsOfType<{ a: number; b: string }, number>>({ a: 2 });
6969
expectAssignable<OnlyFieldsOfType<{ a: number; b: string }, string>>({ b: 'hello' });
7070
expectAssignable<OnlyFieldsOfType<{ a: number; b: string }, string, boolean>>({ b: true });
7171

72+
// test the case in which AssignableType does not inherit from FieldType, and AssignableType is provided
73+
expectAssignable<OnlyFieldsOfType<any, string, boolean>>({ b: false });
74+
7275
// test generic schema, essentially we expect nearly no safety here
7376
expectAssignable<OnlyFieldsOfType<Document, NumericType | undefined>>({ someKey: 2 });
7477
// We can still at least enforce the type that the keys map to

0 commit comments

Comments
 (0)