Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-6639

Improve Strict Typing in StrictUpdateFilter and StrictFilter Types (Avoid Appending `& Document`)

    • Type: Icon: Task Task
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: TypeScript
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      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;
      } 

            Assignee:
            Unassigned Unassigned
            Reporter:
            alex.svatukhin@mongodb.com Alex Svatukhin
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: