Repository navigation
Set mongoose document id property to any - #21856
Conversation
|
types/mongoose/index.d.ts to authors (@horiuchi @sindrenm @lukasz-zak). Could you review this PR? |
|
A definition author has approved this PR ⭐️. A maintainer will merge this PR shortly. If it shouldn't be merged yet, please leave a comment saying so and we'll wait. Thank you for your contribution to DefinitelyTyped! |
|
Someone merge this ASPS, I'm waiting this for deploy...Thanks. |
| interface MongooseDocumentOptionals { | ||
| /** The string version of this documents _id. */ | ||
| id?: string; | ||
| id?: any; |
There was a problem hiding this comment.
The documentation link says this is string?
There was a problem hiding this comment.
There is an _id field which is any, was your intention to use that?
There was a problem hiding this comment.
No, if you initialize a mongoose schema using option { id: false }, the id property can be anything you want.
http://mongoosejs.com/docs/api.html#document_Document-id
There was a problem hiding this comment.
I agree with you about _id, but not id. The documentation seems to indicate id is a string.
There was a problem hiding this comment.
Some user declare id in schema, like: id: number, it conflict. so, I suggest mongoose use mongo _id directly or create itself __id or something like __v, not id.
There was a problem hiding this comment.
I asked in mongoose:
Automattic/mongoose#5911
The id property is a mongoose getter than can be disabled. When disabled it becomes a regular mongoose document property which can be set to anything.
http://mongoosejs.com/docs/guide.html#id
| @@ -1144,7 +1144,7 @@ declare module "mongoose" { | |||
|
|
|||
| interface MongooseDocumentOptionals { | |||
| /** The string version of this documents _id. */ | |||
There was a problem hiding this comment.
What about this comment here? Since the property isn't necessarily a string anymore, perhaps we could describe it further?
There was a problem hiding this comment.
(Also because it doesn't necessarily reference the _id.
There was a problem hiding this comment.
Ahh good point.
The id property can actually be turned off by passing { id: false } when creating a schema.
http://mongoosejs.com/docs/api.html#document_Document-id
The user can then define id and any type so we should allow that.
npm run lint package-name(ortscif notslint.jsonis present).Select one of these and delete the others:
If changing an existing definition:
tslint.jsoncontaining{ "extends": "dtslint/dt.json" }.