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

MongoDB IndexOptions type is missing #10590

Closed
rlawisch opened this issue Aug 23, 2021 · 19 comments
Closed

MongoDB IndexOptions type is missing #10590

rlawisch opened this issue Aug 23, 2021 · 19 comments
Labels
typescript Types or Types-test related issue / Pull Request

Comments

@rlawisch
Copy link

rlawisch commented Aug 23, 2021

Do you want to request a feature or report a bug?
Bug

What is the current behavior? If the current behavior is a bug, please provide the steps to reproduce.
In Schema.index second parameter, { unique: true } was marked as invalid because unique is not a member of Mongoose's IndexOption interface.

import { model, Schema } from "mongoose";

export interface User {
  email?: string;
  password?: string;
  createdAt?: Date;
  updatedAt?: Date;
}

const UserSchema = new Schema<User>(
  {
    email: { type: String, required: true },
    password: { type: String, required: true },
  },
  { timestamps: true }
);

UserSchema.index({ email: 1, campaign: 1 }, { unique: true });

export default model<User>("User", UserSchema);

EDIT: See it on StackBlitz

The Mongoose's interface extends MongoDB's IndexOptions interface, which was not available in @types/mongodb version 3.6.20 resolved as a Mongoose dependency by NPM.
Manually installing an updated package (version 4.0.7) fixed the error.

What is the expected behavior?
Using unique and other index options should be TS-compilable.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

  • Node.js: 14.17.3
  • Mongoose: 5.13.7
  • MongoDB: 4.1.0
  • TypeScript: 4.3.5
@vkarpov15 vkarpov15 added this to the 5.13.8 milestone Aug 23, 2021
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Aug 23, 2021
@hasezoey
Copy link
Collaborator

hasezoey commented Aug 23, 2021

Manually installing an updated package (version 4.0.7) fixed the error.

are you sure about that? because since 4.x mongodb provides official types, making the @types/mongodb a stub package

see UNPKG

@rlawisch
Copy link
Author

Yes, I am. A few seconds after installing it, the error was gone. Also, the VSCode navigation breadcrumbs for the (fixed) interface is node_modules > mongoose > node_modules > @types > mongodb > index.d.ts > IndexOptions, which indicates it is still getting the interface definition from @types/mongodb.
I see MongoDB is providing official types, but I couldn't find IndexOptions among them.

@hasezoey
Copy link
Collaborator

I see MongoDB is providing official types, but I couldn't find IndexOptions among them.

mongodb 4.x is only supported from / since mongoose 6.0
btw, in mongodb's official types its CreateIndexesOptions (which is already changed to in mongoose 6.0)

which indicates it is still getting the interface definition from @types/mongodb.

so what version of @types/mongodb is installed now?

@vkarpov15
Copy link
Collaborator

Since the error is gone, I'll close this for now.

@vkarpov15 vkarpov15 removed this from the 5.13.8 milestone Aug 23, 2021
@rlawisch
Copy link
Author

@hasezoey

so what version of @types/mongodb is installed now?

Version 4.0.7 is installed and has the IndexOptions interface (3.6.20 didn't).

@rlawisch
Copy link
Author

@vkarpov15 It's gone, but was manually fixed. Shouldn't it be fixed for everyone else?
Here's a StackBlitz proof/example

@vkarpov15 vkarpov15 reopened this Aug 23, 2021
@vkarpov15 vkarpov15 added this to the 5.13.9 milestone Aug 23, 2021
@hasezoey
Copy link
Collaborator

@rlawisch in your provided example, if you try to Go to Definition (to open mongoose's index.d.ts) on UserSchema.index and then go to the top of the page, you can see the error Cannot find module 'mongodb' or its corresponding type declarations.(2307), which means that stackblitz is not properly installing (type) packages

@rlawisch
Copy link
Author

@hasezoey yes, my bad, haven't seen this.
I've set up a test project, tried installing/uninstalling different versions, and just intermittently replicated the issue. As I could not replicate it with 5.13.8, I guess @vkarpov15 can close it.
Thanks for your quick response, guys!

@vkarpov15 vkarpov15 removed this from the 5.13.9 milestone Aug 23, 2021
@tehmas
Copy link

tehmas commented Jul 7, 2022

@hasezoey

I'm facing this issue on mongoose 5.13.14 as well. When I use SomeSchema.index({ fieldName: 1 }, { unique: true }), I get error:

Argument of type '{ unique: boolean; }' is not assignable to parameter of type 'IndexOptions'.
  Object literal may only specify known properties, and 'unique' does not exist in type 'IndexOptions'.

When I go to definition of .index(), I came across this:

index(fields: IndexDefinition, options?: IndexOptions): this;

When go to definition of IndexOptions:

  interface IndexOptions extends mongodb.IndexOptions {
    expires?: number | string;
    weights?: AnyObject;
  }

I get the message "No definition found for IndexOptions" when I click "Go to definition" for .IndexOptions of mongodb.

mongodb.d.ts exists in my case. Dependency for mongoose 5.1.3.14 is mongodb 3.7.3.

@hasezoey
Copy link
Collaborator

hasezoey commented Jul 7, 2022

I get the message "No definition found for IndexOptions" when I click "Go to definition" for .IndexOptions of mongodb.

you either dont have @types/mongodb installed, or a version that does not provide any types anymore, try a earlier version

@tehmas
Copy link

tehmas commented Jul 7, 2022 via email

@hasezoey
Copy link
Collaborator

hasezoey commented Jul 7, 2022

By an earlier version do you mean earlier version of mongoose?

no i by an earlier version i mean of @types/mongodb, for that you may also need to manually modify the lockfile and or use a overwrite when possible

@vkarpov15 vkarpov15 reopened this Jul 8, 2022
@vkarpov15 vkarpov15 added this to the 5.13.15 milestone Jul 8, 2022
@hasezoey
Copy link
Collaborator

hasezoey commented Jul 8, 2022

@tehmas could you maybe provide output of yarn why @types/mongodb or npm ls @types/mongodb

@tehmas
Copy link

tehmas commented Jul 8, 2022

@hasezoey output of npm ls @types/mongodb

└─┬ mongoose@5.13.14
  └── @types/mongodb@3.6.20

@hasezoey
Copy link
Collaborator

hasezoey commented Jul 8, 2022

then this is weird, because i cannot get it to re-produce that error, because @types/mongodb is properly installed, IndexOptions should exist, maybe try some of the following:

  • remove your node_modules and re-install
  • upgrade typescript version if possible

@tehmas
Copy link

tehmas commented Jul 8, 2022

@hasezoey Tried both approaches (upgraded typescript to 4.7.4) but it didn't resolve the issue.

I'm still getting the same error which @rlawisch has shared in stackblitz

@hasezoey
Copy link
Collaborator

hasezoey commented Jul 8, 2022

i can see the error in stackblitz (even after upgrading to latest 5.13), but i cannot see the reason why and i cannot reproduce it locally

@titivermeesch
Copy link

titivermeesch commented Jul 14, 2022

My fix was to put this in my package.json:

"resolutions": {
    "@types/mongodb": "3.6.20"
  }

Figured out that some other package was also depending on v4 types, which were apparently not compatible with my mongoose version v5.9.

This fix probably also works with slightly different mongoose and mongodb versions, but those are the ones I used

@vkarpov15 vkarpov15 modified the milestones: 5.13.15, 5.13.16 Aug 22, 2022
@vkarpov15
Copy link
Collaborator

I see the StackBlitz IDE showing a warning, but the project compiles fine, both locally and on StackBlitz. The following screenshot shows that I've made some edits and StackBlitz accepts them.

image

I'm going to chalk this up as a quirk of StackBlitz's IDE, since it looks like this project compiles fine on StackBlitz.

@vkarpov15 vkarpov15 removed this from the 5.13.16 milestone Feb 20, 2023
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

5 participants