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

Generic type cannot be used in Filter

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 4.7.0
    • Component/s: TypeScript
    • 1
    • None
    • Not Needed
    • None
    • None
    • None
    • None
    • None
    • None

      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":

            Assignee:
            neal.beeken@mongodb.com Neal Beeken
            Reporter:
            quint@swydo.com Quint Stoffers
            None
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: