`_id` property from generic does not match filter type definition

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Works as Designed
    • Priority: Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None
    • 0
    • None
    • Not Needed
    • None
    • None
    • None
    • None
    • None
    • None

      What problem are you facing?

      I want to define a generic storage class and use a generic type in the collection definition. Generic constraints do not work for me with filter type definitions.

      All my schemas have common properties and I want to use these properties in filters. For example, all schemas have the `_id` property. I define this property in the interface and specify constrain to the generic type to force the type to extend the interface.

      The generic type should have the property because this is forced by the constraint. But I can not use the property with a filter.

      The following code does not compile.

      interface TypeWithId {
          _id: number;
      {{ }}}
      class Storage<T extends TypeWithId> {
          constructor(private db: Db) {}

          public async find(id: number): Promise<T | null> {
              const schemaForGeneric = this.db.collection<T>('a');
              const result = await schemaForGeneric.findOne({{

      { _id: id }

      }});
              expectType<T | null>(result);
              return result;
      {{    }}}
      {{ }}}

      Below is the compilation error.

      {{ ❯ npm run check:tsd}}> mongodb@4.2.2 check:tsd
      {{ > tsd --version && tsd}}

      0.19.0

        test/types/community/collection/findX.test-d.ts:303:50
        ✖ 303:50 No overload matches this call.
        The last overload gave the following error.
          Argument of type{{

      { id: number; }

      }}is not assignable to parameter of type Filter<T>.
            Type{{

      { id: number; }

      }}is not assignable to type{{

      { [Property in Join<NestedPaths<WithId<T>>, ".">]?: Condition<PropertyType<WithId<T>, Property>> | undefined; }

      }}.
        ✖ 304:25 Argument of type void is not assignable to parameter of type T | null.
        ✖ 305:4 Type void is not assignable to type T | null.3 errors{{~/Projects/node-mongodb-native  on main !1   took 8s  at }}

      What driver and relevant dependency versions are you using?

      The latest code from the `main` branch.

      Steps to reproduce?

      1. Added my test code to the `test/types/community/collection/findX.test-d.ts` file.
      2. Run tests with the `npm run check:tsd` command.

        1. image-2021-12-28-09-35-21-168.png
          66 kB
          Victor Smirnov
        2. image-2021-12-28-09-37-23-067.png
          70 kB
          Victor Smirnov

            Assignee:
            Unassigned
            Reporter:
            Victor Smirnov
            None
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: