diff --git a/package.json b/package.json index d604069cb03..a23195de38f 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "eslint-plugin-markdown": "^3.0.0", "eslint-plugin-mocha-no-only": "1.1.1", "express": "^4.18.1", + "fs-extra": "~11.1.1", "highlight.js": "11.7.0", "lodash.isequal": "4.5.0", "lodash.isequalwith": "4.4.0", @@ -67,8 +68,7 @@ "tsd": "0.25.0", "typescript": "4.9.5", "uuid": "9.0.0", - "webpack": "5.75.0", - "fs-extra": "~11.1.1" + "webpack": "5.75.0" }, "directories": { "lib": "./lib/mongoose" diff --git a/test/types/mongo.test.ts b/test/types/mongo.test.ts new file mode 100644 index 00000000000..f3b22e59f2f --- /dev/null +++ b/test/types/mongo.test.ts @@ -0,0 +1,17 @@ +import * as mongoose from 'mongoose'; +import { expectType } from 'tsd'; +import * as bson from 'bson'; + +function gh12537() { + const schema = new mongoose.Schema({ test: String }); + const model = mongoose.model('Test', schema); + + const doc = new model({}); + + const v = new bson.ObjectId('somehex'); + expectType(v._id.toHexString()); + + doc._id = new bson.ObjectId('somehex'); +} + +gh12537(); diff --git a/types/augmentations.d.ts b/types/augmentations.d.ts new file mode 100644 index 00000000000..fe72cb1aa3f --- /dev/null +++ b/types/augmentations.d.ts @@ -0,0 +1,9 @@ +// this import is required so that types get merged instead of completely overwritten +import 'bson'; + +declare module 'bson' { + interface ObjectId { + /** Mongoose automatically adds a conveniency "_id" getter on the base ObjectId class */ + _id: this; + } +} \ No newline at end of file diff --git a/types/index.d.ts b/types/index.d.ts index ac24805858c..8a12e688a4d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -22,6 +22,7 @@ /// /// /// +/// declare class NativeDate extends global.Date { } diff --git a/types/types.d.ts b/types/types.d.ts index f782b7cf004..75a918c754d 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -80,7 +80,7 @@ declare module 'mongoose' { } class ObjectId extends mongodb.ObjectId { - _id: this; + } class Subdocument extends Document {