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 document type #13999

Closed
2 tasks done
ksyd9821 opened this issue Oct 23, 2023 · 2 comments · Fixed by #14152
Closed
2 tasks done

insertMany return type incompatible with document type #13999

ksyd9821 opened this issue Oct 23, 2023 · 2 comments · Fixed by #14152
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@ksyd9821
Copy link

ksyd9821 commented Oct 23, 2023

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.6.3

Node.js version

18.x

MongoDB server version

6.x

Typescript version (if applicable)

5.2.2

Description

This is the same issue previously reported in #13957
I have included the code that reproduces it below.
Basically with the code below

import * as mongoose from "mongoose";

export class RepositoryBase<T> {
    protected model: mongoose.Model<T & mongoose.Document>;

    constructor(schemaModel: mongoose.Model<T & mongoose.Document>) {
        this.model = schemaModel;
    }

    async insertMany(elems: T[]): Promise<T[]> {
        elems = await this.model.insertMany(elems);
        return elems;
    }
}

the assignment inside the insertMany function generates the following error
image

Steps to Reproduce

https://github.com/ksyd9821/mongoose-insert-many-issue

Expected Behavior

No response

Tasks

No tasks being tracked yet.
@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Oct 23, 2023
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Oct 24, 2023
@vkarpov15 vkarpov15 added this to the 7.6.5 milestone Oct 24, 2023
@Automattic Automattic deleted a comment from donoftime2018 Nov 6, 2023
@vkarpov15
Copy link
Collaborator

Just remove the & Document and this script compiles fine:

import * as mongoose from "mongoose";

export class RepositoryBase<T> {
    protected model: mongoose.Model<T>;

    constructor(schemaModel: mongoose.Model<T>) {
        this.model = schemaModel;
    }

    async insertMany(elems: T[]): Promise<T[]> {
        elems = await this.model.insertMany(elems);
        return elems;
    }
}

As of Mongoose 7, we no longer support using extends Document in the DocType generic param to Model<>, and we recommend using the raw doc type instead. So don't use & Document.

@vkarpov15 vkarpov15 removed this from the 7.6.6 milestone Nov 17, 2023
@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. typescript Types or Types-test related issue / Pull Request labels Nov 17, 2023
@hardcodet
Copy link

hardcodet commented Nov 26, 2023

Hi @vkarpov15

Just stumbled over this one. Is it possible that there is an overload missing here (we're on 8.0.0. currently)?
Your snippet works, but not, if I provide an options object. See the inferred types in the screenshot here - bar here doesn't properly cast to T[], while foo works.

image

Let me know if you'd prefer a new issue. (I also don't know if you'll read this, since it's already closed, so I might open a new issue in a few days anyway).

@vkarpov15 vkarpov15 reopened this Nov 28, 2023
@vkarpov15 vkarpov15 added this to the 8.0.3 milestone Nov 28, 2023
@vkarpov15 vkarpov15 added needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue typescript Types or Types-test related issue / Pull Request and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Nov 28, 2023
vkarpov15 added a commit that referenced this issue Dec 5, 2023
types(model): add no-generic override for `insertMany()` with options
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.

4 participants