MongoDB Driver Filter<Type> errors

I use:
mongodb driver 4.4.1 for NodeJS
And Typescript version 4.3.5

And I faced with type errors in place where they shouldn’t be exists.
I’ve provided some examples.

import { AlternativeType, Document, Filter, PropertyType } from "mongodb";
import { FindCursor, FindOptions, MongoClient, ObjectId, WithId } from "mongodb"

interface BaseDocument {
  _id?: ObjectId;
}

interface MongoWrapperInterface<MongoDocument extends BaseDocument> {
  findByIds(collectionName: string, ids: ReadonlyArray<ObjectId>, options: FindOptions): FindCursor<WithId<MongoDocument>>;
}

const MONGO_URL = "127.0.0.1:27015";
class MongoWrapper<MongoDocument extends BaseDocument> implements MongoWrapperInterface<MongoDocument> {
  private readonly mongoClient: MongoClient;

  constructor() {
    this.mongoClient = new MongoClient(MONGO_URL);
    this.mongoClient.connect(); // it's not related to problem.
  }

  // Compile OKAY
  findByIds(collectionName: string, ids: ReadonlyArray<ObjectId>, options: FindOptions<Document>): FindCursor<WithId<MongoDocument>> {
    return this.mongoClient.db().collection<MongoDocument>(collectionName).find({
      _id: {
        $in: ids as any
      }
    }, options);
  }

  // Compile OKAY
  findByIds(collectionName: string, ids: ReadonlyArray<ObjectId>, options: FindOptions<Document>): FindCursor<WithId<MongoDocument>> {
    return this.mongoClient.db().collection<MongoDocument>(collectionName).find({
      _id: {
        $in: ids
      }
    } as unknown as Filter<MongoDocument>,
    options);
  }

  // Compile OKAY
  findByIds(collectionName: string, ids: ReadonlyArray<ObjectId>, options: FindOptions<Document>): FindCursor<WithId<MongoDocument>> {
    return this.mongoClient.db().collection<MongoDocument>(collectionName).find({
      _id: {
        $in: ids
      }
    } as Filter<BaseDocument> as Filter<MongoDocument>,
    options);
  }

  // Compile OKAY
  findByIds(collectionName: string, ids: ReadonlyArray<ObjectId>, options: FindOptions<Document>): FindCursor<WithId<MongoDocument>> {
    return this.mongoClient.db().collection<MongoDocument>(collectionName).find({
      _id: {
        $in: ids as ReadonlyArray<AlternativeType<PropertyType<WithId<MongoDocument>, '_id'>>>
      }
    } as Filter<MongoDocument>,
    options);
  }

  // Why this doesn't work? (That's how I want to implement)
  // Compile Failed
  findByIds(collectionName: string, ids: ReadonlyArray<ObjectId>, options: FindOptions<Document>): FindCursor<WithId<MongoDocument>> {
    return this.mongoClient.db().collection<MongoDocument>(collectionName).find({
      _id: {
        $in: ids
      }
    } as Filter<MongoDocument>,
    options);
  }
  // Error:
  // Conversion of type '{ _id: { $in: readonly ObjectId[]; }; }' to type 'Filter<MongoDocument>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
  //  Type '{ _id: { $in: readonly ObjectId[]; }; }' is not comparable to type '{ [Property in Join<NestedPaths<WithId<MongoDocument>>, ".">]?: Condition<PropertyType<WithId<MongoDocument>, Property>> | undefined; } & RootFilterOperators<...>'.
  //  Type '{ _id: { $in: readonly ObjectId[]; }; }' is not comparable to type '{ [Property in Join<NestedPaths<WithId<MongoDocument>>, ".">]?: Condition<PropertyType<WithId<MongoDocument>, Property>> | undefined; }'.ts(2352)

  // Compile Failed
  findByIds(collectionName: string, ids: ReadonlyArray<ObjectId>, options: FindOptions<Document>): FindCursor<WithId<MongoDocument>> {
    return this.mongoClient.db().collection<MongoDocument>(collectionName).find({
      _id: {
        $in: ids
      }
    },
    options);
  }
  // Error:
  // No overload matches this call.
  // Overload 1 of 3, '(filter: Filter<MongoDocument>, options?: FindOptions<Document> | undefined): FindCursor<WithId<MongoDocument>>', gave the following error.
  //   Argument of type '{ _id: { $in: readonly ObjectId[]; }; }' is not assignable to parameter of type 'Filter<MongoDocument>'.
  //     Type '{ _id: { $in: readonly ObjectId[]; }; }' is not assignable to type '{ [Property in Join<NestedPaths<WithId<MongoDocument>>, ".">]?: Condition<PropertyType<WithId<MongoDocument>, Property>> | undefined; } & RootFilterOperators<...>'.
  //       Type '{ _id: { $in: readonly ObjectId[]; }; }' is not assignable to type '{ [Property in Join<NestedPaths<WithId<MongoDocument>>, ".">]?: Condition<PropertyType<WithId<MongoDocument>, Property>> | undefined; }'.
  // Overload 2 of 3, '(filter: Filter<MongoDocument>, options?: FindOptions<Document> | undefined): FindCursor<WithId<MongoDocument>>', gave the following error.
  //   Argument of type '{ _id: { $in: readonly ObjectId[]; }; }' is not assignable to parameter of type 'Filter<MongoDocument>'.
  //     Type '{ _id: { $in: readonly ObjectId[]; }; }' is not assignable to type '{ [Property in Join<NestedPaths<WithId<MongoDocument>>, ".">]?: Condition<PropertyType<WithId<MongoDocument>, Property>> | undefined; } & RootFilterOperators<...>'.
  //       Type '{ _id: { $in: readonly ObjectId[]; }; }' is not assignable to type '{ [Property in Join<NestedPaths<WithId<MongoDocument>>, ".">]?: Condition<PropertyType<WithId<MongoDocument>, Property>> | undefined; }'.ts(2769)
}

Whar’s wrong here?

1 Like