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

mongoose.Types.ObjectId.isValid and mongoose.Types.ObjectId.toHexString missing from type definitions #10678

Closed
mstaicu opened this issue Sep 4, 2021 · 4 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@mstaicu
Copy link

mstaicu commented Sep 4, 2021

What is the current behavior?

The mongoose.Types.ObjectId.isValid and mongoose.Types.ObjectId.toHexString types are no longer present in "mongoose": "^5.12.2" ( which resolves to "version": "5.13.8" )

When trying to access any of those properties I get a: Property 'isValid' does not exist on type 'ObjectIdConstructor'.ts(2339)

In mongooses' index.d.ts you got:

const ObjectId: ObjectIdConstructor; pointing to:

// Expose static methods of `mongodb.ObjectID` and allow calling without `new`
    type ObjectIdConstructor = typeof _ObjectId & {
      (val?: string | number): ObjectId;
    };

_ObjectId pointing to:

class _ObjectId extends mongodb.ObjectID {
      _id?: ObjectId;
    }

mongodb.ObjectID pointing to:

export { Binary, DBRef, Decimal128, Double, Int32, Long, MaxKey, MinKey, ObjectID, ObjectId, Timestamp } from "bson";

ObjectID pointing to nothing. @types/bson is a folder that has just the package.json, LICENSE and README. The content of its package.json:

{
    "name": "@types/bson",
    "version": "4.2.0",
    "typings": null,
    "description": "Stub TypeScript definitions entry for bson, which provides its own types definitions",
    "main": "",
    "scripts": {},
    "author": "",
    "license": "MIT",
    "dependencies": {
        "bson": "*"
    }
}

I have an older repo with the same dependencies, i.e. "mongoose": "^5.12.2", and with that I seem to get a @types/bson that has an index.d.ts in its package. The content of its package.json:

{
    "name": "@types/bson",
    "version": "4.0.3",
    "description": "TypeScript definitions for bson",
    "license": "MIT",
    "contributors": [
        {
            "name": "Hiroki Horiuchi",
            "url": "https://github.com/horiuchi",
            "githubUsername": "horiuchi"
        },
        {
            "name": "Federico Caselli",
            "url": "https://github.com/CaselIT",
            "githubUsername": "CaselIT"
        },
        {
            "name": "Justin Grant",
            "url": "https://github.com/justingrant",
            "githubUsername": "justingrant"
        },
        {
            "name": "Mikael Lirbank",
            "url": "https://github.com/lirbank",
            "githubUsername": "lirbank"
        }
    ],
    "main": "",
    "types": "index.d.ts",
    "repository": {
        "type": "git",
        "url": "https://github.com/DefinitelyTyped/DefinitelyTyped.git",
        "directory": "types/bson"
    },
    "scripts": {},
    "dependencies": {
        "@types/node": "*"
    },
    "typesPublisherContentHash": "de339c6ae3194d7c34d771d4580048f1007707bafc646731b539c3a79638ebe9",
    "typeScriptVersion": "3.2"
}

Whats the solution here? I tried locking down mongoose to a specific version in order to get its dependencies to install, but that didn't work. NPM states that the types for BSON are no longer an independent package. Thoughts?

PS: bson does not provide its own types, as per the description of the latest @types/bson

Also, mongoose requires "@types/mongodb": "^3.5.27" but in my working repo that resolves to

 "@types/mongodb": {
      "version": "3.6.18",
      "resolved": "https://registry.npmjs.org/@types/mongodb/-/mongodb-3.6.18.tgz",
      "integrity": "sha512-JSVFt9p0rTfZ4EgzXmVHUB3ue00xe3CRbQho8nXfImzEDDM4O7I3po1bwbWl/EIbLENxUreZxqLOc8lvcnLVPA==",
      "requires": {
        "@types/bson": "*",
        "@types/node": "*"
      }
    },

And in the repo that this doesn't work, it resolves to

"@types/mongodb": {
      "version": "3.6.20",
      "resolved": "https://registry.npmjs.org/@types/mongodb/-/mongodb-3.6.20.tgz",
      "integrity": "sha512-WcdpPJCakFzcWWD9juKoZbRtQxKIMYF/JIAM4JrNHrMcnJL6/a2NWjXxW7fo9hxboxxkg+icff8d7+WIEvKgYQ==",
      "requires": {
        "@types/bson": "*",
        "@types/node": "*"
      }
    },

Could it be that @types/mongodb broke the camel's back?

Digging further, it seems that when you go to the landing page on NPM for the mongodb types, it says that mongo provides its own types, but when I go in my dependency tree, there is no such type definition file provided by mongodb, so what is going on?

@mstaicu
Copy link
Author

mstaicu commented Sep 4, 2021

As a temporary solution I decided to go ahead and install as a development dependency the 4.0.3 version of @types/bson
Also, it seems that it goes from @types/bson@4.0.5, which still has the type definitions, straight to @types/bson@4.2.0, which doesn't have the type definitions

@mstaicu mstaicu changed the title mongoose.Types.ObjectId.isValid and mongoose.Types.ObjectId.toHexString missing from mongooses transitive dependency mongoose.Types.ObjectId.isValid and mongoose.Types.ObjectId.toHexString missing type definitions Sep 4, 2021
@mstaicu mstaicu changed the title mongoose.Types.ObjectId.isValid and mongoose.Types.ObjectId.toHexString missing type definitions mongoose.Types.ObjectId.isValid and mongoose.Types.ObjectId.toHexString missing from type definitions Sep 4, 2021
@vkarpov15 vkarpov15 added this to the 5.13.9 milestone Sep 6, 2021
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Sep 6, 2021
@vkarpov15
Copy link
Collaborator

Confirmed this is an issue with the latest version of @types/bson, apparently they removed the entire contents of the package in a minor release 🤷‍♂️

Should be fixed by fc5fc7e, please upgrade to v5.13.9 for the fix. We'll be releasing v5.13.9 shortly.

@mstaicu
Copy link
Author

mstaicu commented Sep 7, 2021

Thanks @vkarpov15 !

@firehudson
Copy link

I'm trying to migrate from mongoose v4 to v6 and I came across the same issue.

Not sure if it's still helpful, but mongoose now have moved this method from ObjectId to mongoose itself.
So simply use mongoose.isValidObjectId(...) 😃

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

3 participants