Skip to content

Commit

Permalink
Merge pull request #13723 from Automattic/IslandRhythms/backport-13515
Browse files Browse the repository at this point in the history
backport pull 13515
  • Loading branch information
vkarpov15 committed Aug 11, 2023
2 parents d4a1080 + 557a472 commit c1109ac
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
17 changes: 17 additions & 0 deletions 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<string>(v._id.toHexString());

doc._id = new bson.ObjectId('somehex');
}

gh12537();
9 changes: 9 additions & 0 deletions 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;
}
}
1 change: 1 addition & 0 deletions types/index.d.ts
Expand Up @@ -22,6 +22,7 @@
/// <reference path="./validation.d.ts" />
/// <reference path="./inferschematype.d.ts" />
/// <reference path="./virtuals.d.ts" />
/// <reference path="./augmentations.d.ts" />

declare class NativeDate extends global.Date { }

Expand Down
2 changes: 1 addition & 1 deletion types/types.d.ts
Expand Up @@ -80,7 +80,7 @@ declare module 'mongoose' {
}

class ObjectId extends mongodb.ObjectId {
_id: this;

}

class Subdocument<IdType = any> extends Document<IdType> {
Expand Down

0 comments on commit c1109ac

Please sign in to comment.