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

Investigate NODE-4305 - Generic type cannot be used in Filter

    XMLWordPrintableJSON

Details

    • Task
    • Status: Closed
    • Unknown
    • Resolution: Done
    • None
    • None
    • None
    • Not Needed

    Description

      NODE-4305 Description

      What problem are you facing?

      When using a generic type, the type of `Filter` seems to break down. This seems to be isolated to when using a generic type, as "hardcoding" the type works fine.

      What driver and relevant dependency versions are you using?

      "mongodb": "4.7.0"

      "typescript": "4.6.4"

      Steps to reproduce?

      async function demo<T extends { foo: string }>() {
          const client = await MongoClient.connect('mongodb://xyz');
       
          const col = client.db().collection<T>('foo');
          col.findOne({ foo: 'foo' });
      }

      Results in

      TS2769: No overload matches this call.   The last overload gave the following error.     Argument of type '{ foo: "foo"; }' is not assignable to parameter of type 'Filter<T>'.       Type '{ foo: "foo"; }' is not assignable to type '{ [Property in Join<NestedPaths<WithId<T>>, ".">]?: Condition<PropertyType<WithId<T>, Property>> | undefined; } & RootFilterOperators<...>'.         Type '{ foo: "foo"; }' is not assignable to type '{ [Property in Join<NestedPaths<WithId<T>>, ".">]?: Condition<PropertyType<WithId<T>, Property>> | undefined; }'.

      Whereas "hardcoding" the type works fine

      async function demo<>() {
          const client = await MongoClient.connect('mongodb://xyz');
       
          const col = client.db().collection<{ foo: string }>('foo');
          col.findOne({ foo: 'foo' });
      }

      Oddly enough, autocomplete seems to provide the correct type for "foo":

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              dbeng-pm-bot PM Bot
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: