Skip to content

Commit 6974918

Browse files
committed
Re-add unsafe compatability with unreleased root operators
From [@dariakp](https://github.com/dariakp): > ... the reason we have RootFilterOperators extend Document is so that > any driver version can be forwards compatible with new query operators > that are continually being added to the server. There is an > improvement that we would love to make to only extend $-prefixed keys > instead of the generic Document (since operators are always > $-prefixed), but we haven't had a chance to look into making that > work. Here we introduce such a type, but in a much more restricted format than the `BSON.Document` type formerly extended: - This is scoped explicitly to keys prefixed with `$` - We can use the `unknown` type instead of `any`; in the event that a user is attempting to extract a value from an object typed as a `Filter<TSchema>` and use it somewhere they will be notified that it is not type safe and required to use a type cast or other workaround. Follow-ups: - Consider preferring safety of existing types over compatibility with with future operators (https://jira.mongodb.org/browse/NODE-3904)
1 parent bbb1a6b commit 6974918

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/mongo_types.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,23 @@ export type AlternativeType<T> = T extends ReadonlyArray<infer U>
9191
/** @public */
9292
export type RegExpOrString<T> = T extends string ? BSONRegExp | RegExp | T : T;
9393

94+
/**
95+
* This is a type that allows any `$` prefixed keys and makes no assumptions
96+
* about their value types. This stems from a design decision that newly added
97+
* filter operators should be accepted without needing to upgrade this package.
98+
*
99+
* This has the unfortunate side effect of preventing type errors on unknown
100+
* operator keys, so we should prefer not to extend this type whenever possible.
101+
*
102+
* @see https://github.com/mongodb/node-mongodb-native/pull/3115#issuecomment-1021303302
103+
* @public
104+
*/
105+
export type OpenOperatorQuery = {
106+
[key: `$${string}`]: unknown;
107+
};
108+
94109
/** @public */
95-
export interface RootFilterOperators<TSchema> {
110+
export interface RootFilterOperators<TSchema> extends OpenOperatorQuery {
96111
$and?: Filter<TSchema>[];
97112
$nor?: Filter<TSchema>[];
98113
$or?: Filter<TSchema>[];

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ expectNotType<Filter<PetModel>>({ name: { $or: ['Spot', 'Bubbles'] } });
288288
/// it should not accept single objects for __$and, $or, $nor operator__ query
289289
expectNotType<Filter<PetModel>>({ $and: { name: 'Spot' } });
290290

291+
/// it allows using unknown root operators with any value
292+
expectAssignable<Filter<PetModel>>({ $fakeOp: 123 });
293+
291294
/**
292295
* test 'element' query operators
293296
*/

0 commit comments

Comments
 (0)