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

insertMany return type incompatible with InsertManyResult<T> (TypeScript) #13904

Closed
2 tasks done
andrii-kasparevych opened this issue Sep 28, 2023 · 1 comment · Fixed by #13965
Closed
2 tasks done
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@andrii-kasparevych
Copy link

Prerequisites

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

Mongoose version

7.5.3

Node.js version

v18.13

MongoDB server version

7.x

Typescript version (if applicable)

No response

Description

While migrating a NestJs/TypeScript project from mongoose v6.10.3 to v7.5.3, I have faced a problem with the return type of Model.insertMany method. I have a method handling creating many records and then reporting the errors/duplicated/created documents, where I directly use InsertManyResult type from mongoose, and the typed assignment like below is crucial.

const insertManyResult: InsertManyResult<ITest> = await Test.insertMany(
      [{ name: "test" }],
      {
        ordered: false,
        rawResult: true
      }
    );

The compile error boils down to:

...        Type 'Error' is not assignable to type 'CastError | ValidatorError'.
          Type 'MongooseError' is missing the following properties from type 'ValidatorError': properties, kind, path, valuets(2322)

Similar code worked in v6.10.3, but this commit has introduced a new overload for insertMany which is as follows:

    insertMany<DocContents = TRawDocType>(
      doc: DocContents | TRawDocType,
      options: InsertManyOptions & { ordered: false; rawResult: true; }
    ): Promise<mongodb.InsertManyResult<Require_id<DocContents>> & {
      mongoose: {
        validationErrors: Error[];
        results: Array<
          Error |
          Object |
          MergeType<THydratedDocumentType, DocContents>
        >
      }
    }>;

while the exported InsertManyResult type looks like this:

  type InsertManyResult<T> = mongodb.InsertManyResult<T> & {
    insertedIds: {
      [key: number]: InferId<T>;
    };
    mongoose?: { validationErrors?: Array<Error.CastError | Error.ValidatorError> };
  };

And the error is caused by validationErrors: Error[] being incompatible with validationErrors?: Array<Error.CastError | Error.ValidatorError>.

While I could fix the issue by adding a custom type

type InsertManyResultFixed<T> = Omit<InsertManyResult<T>, 'mongoose'> & {
  mongoose: {
    validationErrors: Error[];
  };
};

I would prefer to use the library types.

Steps to Reproduce

import { InsertManyResult, Schema, model } from "mongoose";

const schema = new Schema({ name: String });

interface ITest {
  name?: string;
}
const Test = model<ITest>("Test", schema);

export class InsertManyDemo {
  public async createMany() {

     // the assignment below gives compile error
    const insertManyResult: InsertManyResult<ITest> = await Test.insertMany(
      [{ name: "test" }],
      {
        ordered: false,
        rawResult: true
      }
    );
    return insertManyResult;
  }
}

Link to the sandbox with demo: https://codesandbox.io/s/typescript-playground-forked-9mjzg5?file=/src/index.ts

Expected Behavior

The code compiles and InsertManyResult<T> type actually represents what 'insertMany' returns.

@scabezas632
Copy link

Same problem here, I had to downgrade to 7.5.2 version

@vkarpov15 vkarpov15 added this to the 7.5.5 milestone Oct 1, 2023
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Oct 1, 2023
vkarpov15 added a commit that referenced this issue Oct 11, 2023
types(model): make InsertManyResult consistent with return type of insertMany
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

Successfully merging a pull request may close this issue.

3 participants