Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-3681): Typescript error in Collection.findOneAndModify UpdateFilter $currentDate #4047

Merged
merged 10 commits into from
Mar 21, 2024
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
Loading