From a8670a76cd2746e8353504c100050b474d844f36 Mon Sep 17 00:00:00 2001 From: Aditi Khare <106987683+aditi-khare-mongoDB@users.noreply.github.com> Date: Thu, 21 Mar 2024 16:24:30 -0400 Subject: [PATCH] fix(NODE-3681): Typescript error in Collection.findOneAndModify UpdateFilter $currentDate (#4047) --- src/mongo_types.ts | 2 +- test/types/community/collection/updateX.test-d.ts | 7 +++++++ test/types/helper_types.test-d.ts | 3 +++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/mongo_types.ts b/src/mongo_types.ts index 1924b2afe2..0f878041e0 100644 --- a/src/mongo_types.ts +++ b/src/mongo_types.ts @@ -219,7 +219,7 @@ export type NotAcceptedFields = { /** @public */ export type OnlyFieldsOfType = IsAny< TSchema[keyof TSchema], - Record, + AssignableType extends FieldType ? Record : Record, AcceptedFields & NotAcceptedFields & Record diff --git a/test/types/community/collection/updateX.test-d.ts b/test/types/community/collection/updateX.test-d.ts index 5ea59cd525..c2fb170b74 100644 --- a/test/types/community/collection/updateX.test-d.ts +++ b/test/types/community/collection/updateX.test-d.ts @@ -60,6 +60,13 @@ expectAssignable>({ $inc: { anyKeyWhatsoever: 2 } }); // But this no longer asserts anything about what the original keys map to expectNotType>({ $inc: { numberField: '2' } }); +expectAssignable>({ + $currentDate: { anyKeyWhatsoever: { $type: 'timestamp' } } +}); +expectAssignable>({ $currentDate: { anyKeyWhatsoever: { $type: 'date' } } }); +expectAssignable>({ $currentDate: { anyKeyWhatsoever: true } }); +expectNotType>({ $currentDate: { anyKeyWhatsoever: 'true' } }); +expectNotType>({ $currentDate: { anyKeyWhatsoever: { key2: true } } }); // collection.updateX tests const client = new MongoClient(''); const db = client.db('test'); diff --git a/test/types/helper_types.test-d.ts b/test/types/helper_types.test-d.ts index f5df10de56..ce8fbfce0f 100644 --- a/test/types/helper_types.test-d.ts +++ b/test/types/helper_types.test-d.ts @@ -69,6 +69,9 @@ expectAssignable>({ a: 2 }); expectAssignable>({ b: 'hello' }); expectAssignable>({ b: true }); +// test the case in which AssignableType does not inherit from FieldType, and AssignableType is provided +expectAssignable>({ b: false }); + // test generic schema, essentially we expect nearly no safety here expectAssignable>({ someKey: 2 }); // We can still at least enforce the type that the keys map to