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

BSON ObjectId is not compatible with Types.ObjectId #13712

Closed
2 tasks done
zhi-qiu-yi opened this issue Aug 9, 2023 · 1 comment
Closed
2 tasks done

BSON ObjectId is not compatible with Types.ObjectId #13712

zhi-qiu-yi opened this issue Aug 9, 2023 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@zhi-qiu-yi
Copy link

zhi-qiu-yi commented Aug 9, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.11.5

Node.js version

18.13.0

MongoDB server version

4.16.0

Typescript version (if applicable)

4.9.5

Description

import { ObjectId } from 'mongodb';
import { Types } from 'mongoose';

const objectId: Types.ObjectId = new ObjectId(); // TS2741: Property '_id' is missing in type 'import("~/node_modules/bson/bson").ObjectID' but required in type 'import("mongoose").Types.ObjectId'. 

I see the same issue #12537 , and someone fixed it with #13515 on v7.4.0, but not on v6.x.
Since v6.0.0 produced this bug(commit link), why not fix it on v6.x? It's more difficult to migrate to v7.x.

Steps to Reproduce

it's in the description above, or see the same bug issue #12537

Expected Behavior

Fix it on v6.x and suggestions:

Suggestion 1: Back to the original definition

Removing the _id property may not be a good thing:
image

Source code(code link):
image

The original definition(commit link) seems more reasonable, and it's compatible with bson.ObjectId:
image

Suggestion 2: Removing the magic _id on Types.ObjectId

image

That's convenience for people using populate, but that's not friendly to people using Types.ObjectId and bson.ObjectId, too confusing.

There may be another way to make it happen on populate, but modifying the base ObjectId, sounds not to be a good idea.

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Aug 9, 2023
@IslandRhythms
Copy link
Collaborator

npx tsc --strict 13712.ts
13712.ts:4:7 - error TS2739: Type 'ObjectId' is missing the following properties from type 'ObjectId': _id, generationTime

4 const objectId: Types.ObjectId = new ObjectId();
        ~~~~~~~~


Found 1 error in 13712.ts:4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

3 participants