Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: typing error on bulkWrite with AnyBulkWriteOperation<T> #11985

Closed
2 tasks done
andreialecu opened this issue Jun 24, 2022 · 10 comments
Closed
2 tasks done

types: typing error on bulkWrite with AnyBulkWriteOperation<T> #11985

andreialecu opened this issue Jun 24, 2022 · 10 comments
Labels
typescript Types or Types-test related issue / Pull Request

Comments

@andreialecu
Copy link
Contributor

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.4.0

Node.js version

16

MongoDB server version

5

Description

  1. bulkWrite will show a typescript error if passed a variable of type AnyBulkWriteOperation<ISchema>[]

Steps to Reproduce

function modelBulkTest() {
  interface ISchema {
    _id: string;
    num: number;
  }

  const schema = new Schema({
    _id: { type: String },
    num: Number
  });

  const M = model<ISchema>('Test', schema);

  const ops: AnyBulkWriteOperation<ISchema>[] = [
    {
      updateOne: {
        filter: { _id: 'test' },
        update: {
          $inc: { num: 1 }
        },
        upsert: true
      }
    }
  ];
  M.bulkWrite(ops);
}
const ops: AnyBulkWriteOperation<ISchema>[]
Argument of type 'AnyBulkWriteOperation<ISchema>[]' is not assignable to parameter of type 'AnyBulkWriteOperation<Document>[]'.
  Type 'AnyBulkWriteOperation<ISchema>' is not assignable to type 'AnyBulkWriteOperation<Document>'.
    Type '{ insertOne: InsertOneModel<ISchema>; }' is not assignable to type 'AnyBulkWriteOperation<Document>'.
      Type '{ insertOne: InsertOneModel<ISchema>; }' is not assignable to type '{ insertOne: InsertOneModel<Document>; }'.
        The types of 'insertOne.document' are incompatible between these types.
          Type 'OptionalId<ISchema>' is not assignable to type 'OptionalId<Document>'.
            Type 'OptionalId<ISchema>' is not assignable to type '{ _id?: ObjectId | undefined; }'.
              Types of property '_id' are incompatible.
                Type 'string | undefined' is not assignable to type 'ObjectId | undefined'.
                  Type 'string' is not assignable to type 'ObjectId'.

Expected Behavior

The test should pass.

@vkarpov15 vkarpov15 added this to the 6.4.2 milestone Jun 26, 2022
@IslandRhythms IslandRhythms added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Jun 27, 2022
@IslandRhythms
Copy link
Collaborator

Please include your imports on future issue submissions

import * as mongoose from 'mongoose';
import * as mongodb from 'mongodb';

function modelBulkTest() {
  interface ISchema {
    _id: string;
    num: number;
  }

  const schema = new mongoose.Schema({
    _id: { type: String },
    num: Number
  });

  const M = mongoose.model<ISchema>('Test', schema);

  const ops: mongodb.AnyBulkWriteOperation<ISchema>[] = [
    {
      updateOne: {
        filter: { _id: 'test' },
        update: {
          $inc: { num: 1 }
        },
        upsert: true
      }
    }
  ];
  M.bulkWrite(ops);
}

modelBulkTest();

@vkarpov15 vkarpov15 removed this from the 6.4.2 milestone Jul 1, 2022
@github-actions
Copy link

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the Stale label Jul 16, 2022
@RubenatorX
Copy link

We have this problem as well. What can we provide to help you reproduce? And anything we can do to bandaid the problem?

@github-actions github-actions bot removed the Stale label Jul 19, 2022
@vkarpov15
Copy link
Collaborator

@RubenatorX a standalone a code sample that repros the issue you're seeing. Ideally please open a new issue and follow the issue template, but at least a code sample would be helpful.

@keyCat
Copy link

keyCat commented Jul 21, 2022

We have this issue, as well.
The code sample from the author demonstrates it perfectly, IMO.

I will attempt to describe the reason behind it, and I hope you find it useful:

Type AnyBulkWriteOperation<TSchema extends Document = Document> is a generic, however, .bulkWrite() method has following overloads:

interface Model<T, TQueryHelpers = {}, TMethodsAndOverrides = {}, TVirtuals = {}, TSchema = any> extends
  NodeJS.EventEmitter,
  AcceptsDiscriminator,
  IndexManager,
  SessionStarter {
  // ...
  bulkWrite(writes: Array<mongodb.AnyBulkWriteOperation>, options: mongodb.BulkWriteOptions & MongooseBulkWriteOptions, callback: Callback<mongodb.BulkWriteResult>): void;
  bulkWrite(writes: Array<mongodb.AnyBulkWriteOperation>, callback: Callback<mongodb.BulkWriteResult>): void;
  bulkWrite(writes: Array<mongodb.AnyBulkWriteOperation>, options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions): Promise<mongodb.BulkWriteResult>;
  // ...

As you can see, a type is not supplied (and cannot be supplied) to AnyBulkWriteOperation generic and it becomes locked to AnyBulkWriteOperation<Document> type for each and every model method.

@vkarpov15
Copy link
Collaborator

@keyCat so the issue you're seeing is that writes typing isn't sufficiently strict because AnyBulkWriteOperation takes any?

@keyCat
Copy link

keyCat commented Aug 2, 2022

@vkarpov15 The issue is that AnyBulkWriteOperation treated AnyBulkWriteOperation<Document> for each and every model and unable to accept anything, except for AnyBulkWriteOperation<Document> or any, while it should be able to accept AnyBulkWriteOperation<TSchema> by default.

@vkarpov15
Copy link
Collaborator

Should be fixed by #12167

@vkarpov15 vkarpov15 added typescript Types or Types-test related issue / Pull Request and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Aug 7, 2022
@Kezzsim
Copy link

Kezzsim commented Mar 15, 2023

Screen Shot 2023-03-15 at 4 01 57 PM

Hi, I just upgraded to 7.0.2 and suddenly I have this problem, code that was working just fine demands to be typed as this MongoDB type, but I've tried many configurations for getting it to realize that the data is already structured correctly and none of them work. Is there a way to bypass this? I've tried using "as AnyBulkWriteOperation" and "as AnyBulkWriteOperation" it never works

@vkarpov15
Copy link
Collaborator

@Kezzsim can you please open a new issue and follow the issue template? It is hard to figure out what's going on from an isolated screenshot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

6 participants