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

backport pull 13515 #13723

Merged
merged 3 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
}
}

Check warning on line 9 in types/augmentations.d.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Newline required at end of file but not found
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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