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

Investigate NODE-4587 - Typescript compile error for UpdateFilter and property of type array

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • 2
    • Not Needed

      NODE-4587 Description

      What problem are you facing?

      We are seeing new typescript compilation errors, most probably related to this PR: https://github.com/mongodb/node-mongodb-native/pull/3259

      Here's a stripped down code sample that showcases the error:

      import { UpdateFilter } from 'mongodb';
      
      interface MyType {
        users: string[];
      }
      interface MySimilarType {
        users: string[];
      }
      
      const x: MySimilarType = {
        users: ['bob', 'jane'],
      };
      
      const update: UpdateFilter<MyType> = {
        $set: x,
      };
       

      Here's the compilation error message:

      src/sample.ts:15:3 - error TS2322: Type 'MySimilarType' is not assignable to type 'Readonly<{ [x: `users.${number}`]: string | undefined; [x: `users.$[${string}]`]: string | undefined; users?: string[] | undefined; "users.$"?: string | undefined; }>'.
        Index signature for type '`users.${number}`' is missing in type 'MySimilarType'.15   $set: x,
           ~~~~  node_modules/mongodb/mongodb.d.ts:6347:5
          6347     $set?: MatchKeysAndValues<TSchema>;
                   ~~~~
          The expected type comes from property '$set' which is declared here on type 'UpdateFilter<MyType>'
       

      It appears that even if `MyType` and `MySimilarType` have compatible interfaces, the compiler complains that we cannot assign a `MySimilarType` variable to the `$set` attribute of a `UpdateFilter<MyType>`. Weirdly, if I just remove the type assignment for `x`, the compiler is able to infer that it is compatible and the build succeeds:

      interface MyType {
        users: string[];
      }
      
      const x = {
        users: ['bob', 'jane'],
      };
      
      const update: UpdateFilter<MyType> = {
        $set: x,
      }; 

      What driver and relevant dependency versions are you using?

      We start to see this compilation error from version 4.8.0 of the node.js mongodb driver. I'm using typescript 4.8.2.

      Steps to reproduce?

            Assignee:
            daria.pardue@mongodb.com Daria Pardue
            Reporter:
            dbeng-pm-bot PM Bot
            Daria Pardue
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: