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

Deduced type of an array in a schema is plain array instead of mongoose array. #14983

Open
1 task done
DavidTelenko opened this issue Oct 24, 2024 · 3 comments
Open
1 task done
Labels
backwards-breaking typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@DavidTelenko
Copy link

DavidTelenko commented Oct 24, 2024

Prerequisites

  • I have written a descriptive issue title

Mongoose version

8.6.2

Node.js version

20.18.0

MongoDB version

7.0.14

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

10.0.22631

Issue

Here is simple example of schema in mongoose:

const ExampleSchema = new Schema(
  {
    names: [
      {
        type: String,
      },
    ],
  },
  {
    methods: {
      addToNames: async function (name: string) {
        // this.updateOne() // accessible
        this.names.addToSet(name); // Property 'addToSet' does not exist on type 'string[]'. [2339]
      },
    },
  },
);

this object in a methods is a hydrated document, however array deduced not as mongoose array but as plain array instead. Example demonstrates this issue by trying to use addToSet method of a mongoose array, but getting type error.

Same issue can be depicted with this example:

type IExampleDocument = HydratedDocumentFromSchema<typeof ExampleSchema>;
const Example = model<IExampleDocument>("example", ExampleSchema);
const example = await Example.create({ names: ["hello"] });
example.names.addToSet("example"); // Property 'addToSet' does not exist on type 'string[]'. [2339]

I have pretty vague understanding of how schemas in typescript should be properly defined, I've read that we can provide interfaces ourselves by using template parameters of model and Schema however it essentially produces a duplication in a boilerplate types and defies already defined principles of deduction established with HydratedDocumentFromSchema, InferSchemaType and other typescript helper functions.

@DavidTelenko DavidTelenko added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Oct 24, 2024
Copy link

github-actions bot commented Nov 8, 2024

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 Nov 8, 2024
@vkarpov15 vkarpov15 removed the Stale label Nov 10, 2024
@vkarpov15
Copy link
Collaborator

It looks like this issue isn't limited to methods, you also get the same error with:

const TestModel = model('Test', ExampleSchema);

const doc = new TestModel({});
doc.names.addToSet('taco');

I'm investigating whether we can fix this without a breaking change.

@vkarpov15
Copy link
Collaborator

Unfortunately there isn't much we can do about this without breaking changes. However, we will make this change for Mongoose 9.

@vkarpov15 vkarpov15 modified the milestones: 8.9.1, 9.0 Dec 15, 2024
@vkarpov15 vkarpov15 added backwards-breaking 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 help wanted labels Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backwards-breaking typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

2 participants