From 0ae1367c94487ce703e91c3cd7198fc0f5feb26e Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Fri, 11 Aug 2023 12:04:07 -0400 Subject: [PATCH 1/3] backport pull 13515 --- package.json | 6 +++--- test/types/mongo.test.ts | 19 +++++++++++++++++++ types/augmentations.d.ts | 9 +++++++++ types/index.d.ts | 1 + types/types.d.ts | 2 +- 5 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 test/types/mongo.test.ts create mode 100644 types/augmentations.d.ts diff --git a/package.json b/package.json index d604069cb03..4298067c771 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", @@ -64,11 +65,10 @@ "q": "1.5.1", "sinon": "15.0.1", "stream-browserify": "3.0.0", - "tsd": "0.25.0", + "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..aef7b533b44 --- /dev/null +++ b/test/types/mongo.test.ts @@ -0,0 +1,19 @@ +import * as mongoose from 'mongoose'; +import { expectType } from 'tsd'; +import * as bson from 'bson'; + +import GridFSBucket = mongoose.mongo.GridFSBucket; + +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(); \ No newline at end of file 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 { From bab0e9e3fb069988942106da6ba3c94ecb8a37c4 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 11 Aug 2023 12:08:25 -0400 Subject: [PATCH 2/3] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4298067c771..a23195de38f 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,7 @@ "q": "1.5.1", "sinon": "15.0.1", "stream-browserify": "3.0.0", - "tsd": "^0.25.0", + "tsd": "0.25.0", "typescript": "4.9.5", "uuid": "9.0.0", "webpack": "5.75.0" From 557a472c6046d89c44aa291514339d670b61a65f Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 11 Aug 2023 12:11:20 -0400 Subject: [PATCH 3/3] Update mongo.test.ts --- test/types/mongo.test.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/types/mongo.test.ts b/test/types/mongo.test.ts index aef7b533b44..f3b22e59f2f 100644 --- a/test/types/mongo.test.ts +++ b/test/types/mongo.test.ts @@ -2,8 +2,6 @@ import * as mongoose from 'mongoose'; import { expectType } from 'tsd'; import * as bson from 'bson'; -import GridFSBucket = mongoose.mongo.GridFSBucket; - function gh12537() { const schema = new mongoose.Schema({ test: String }); const model = mongoose.model('Test', schema); @@ -16,4 +14,4 @@ function gh12537() { doc._id = new bson.ObjectId('somehex'); } -gh12537(); \ No newline at end of file +gh12537();