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

TypeScript: Schema.issues() return type is incorrect #13702

Closed
2 tasks done
garyposter opened this issue Aug 5, 2023 · 0 comments · Fixed by #13718
Closed
2 tasks done

TypeScript: Schema.issues() return type is incorrect #13702

garyposter opened this issue Aug 5, 2023 · 0 comments · Fixed by #13718
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@garyposter
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

5.11.0 through today

Node.js version

not pertinent

MongoDB server version

not pertinent

Typescript version (if applicable)

not pertinent

Description

Hi. The docs and code (see below) for schema.prototype.issues say that the result should be an array of [Index Definition, Index Options] pairs.

But the type definition shows that it returns an array of [IndexDefinition].

indexes(): Array<IndexDefinition>;

I think it should be indexes(): Array<[IndexDefinition, IndexOptions]>.

Code references:

Steps to Reproduce

Try writing the example plugin in Typescript.

function myPlugin(schema:Schema) {
  for (const index of schema.indexes()) {
    if (index[1].unique === undefined) {
      index[1].unique = true;
    }
  }
}

It will complain about numerous things. For example, "Property 'unique' does not exist on type 'IndexDirection'."
and "Property 'unique' does not exist on type '1'"

This makes Typescript happy.

  function myPlugin(schema:Schema) {
    const indexes = schema.indexes() as unknown as Array<[IndexDefinition, IndexOptions]>;
    for (const index of indexes) {
      if (index[1].unique === undefined) {
        index[1].unique = true;
      }
    }
  }

It would be nice if that were unnecessary.

Thank you!

Expected Behavior

The following does not generate complaints in TypeScript.

  function myPlugin(schema:Schema) {
    const indexes = schema.indexes();
    for (const index of indexes) {
      if (index[1].unique === undefined) {
        index[1].unique = true;
      }
    }
  }
@garyposter garyposter changed the title TypeScript: Schema.issues() return type incorrect TypeScript: Schema.issues() return type is incorrect Aug 5, 2023
@vkarpov15 vkarpov15 added this to the 7.4.4 milestone Aug 6, 2023
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Aug 6, 2023
@vkarpov15 vkarpov15 modified the milestones: 7.4.4, 7.4.3 Aug 10, 2023
vkarpov15 added a commit that referenced this issue Aug 10, 2023
types(schema): correct return type for `Schema.prototype.indexes()`
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.

2 participants