-
Type: Task
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: TypeScript
A customer raised a concern regarding the typing behavior of the StrictUpdateFilter and StrictFilter types in the node-mongodb-native driver. Both types, as they are currently defined, append & Document, which effectively disables the strict typing by allowing arbitrary fields beyond the defined update operators.
It would make more sense to remove & Document extension, which would provide the behaviour that is expected from the strict filters in question.
The current impact is that when trying to use a property that does not exist, it won't complain as it theoretically should:
expectError<StrictFilter<TestModel>>({
fieldDoesNotExist: 'asdf'
});
Similarly, the following tests would not be valid:
interface TestModel { // other types ... stringLiteralUnion: 'blue' | 'green'; } expectError<StrictUpdateFilter<TestModel>>({ 'stringLiteralUnion': 'yellow' }); expectError<StrictUpdateFilter<TestModel>>({ propertyDoesNotExist: 'bad-property-update' });
Additionally, StrictFilter also uses RootFilterOperators which extends Document as well, leading to similar problems:
export type StrictFilter<TSchema> =
| Partial<TSchema>
| ({
[Property in Join<NestedPaths<WithId<TSchema>, []>, '.'>]?: Condition<
PropertyType<WithId<TSchema>, Property>
>;
} & RootFilterOperators<WithId<TSchema>>);
/** @public */ export interface RootFilterOperators<TSchema> extends Document { // <-- extend document is the problem $and?: Filter<TSchema>[]; $nor?: Filter<TSchema>[]; $or?: Filter<TSchema>[]; $text?: { $search: string; $language?: string; $caseSensitive?: boolean; $diacriticSensitive?: boolean; }; $where?: string | ((this: TSchema) => boolean); $comment?: string | Document; }
- related to
-
NODE-4367 make the filter type strict by default
- Backlog