ARTICLE AD BOX
I'm trying to generate a compatible type for Mongoose/bson ObjectId for a part of my repo which holds types which is shared between my Frontend and my Backend.
I don't want to install/load mongoose or bson in my frontend.
Mongoose version: 9.1.3
This is sample code to reproduce the issue:
// Compatible ObjectId Type in shared Code type InspectFn = (x: unknown, options?: unknown) => string interface ObjectIdLike { id: string | Uint8Array __id?: string toHexString(): string } const bsonType: unique symbol = undefined as any export declare class ObjectId { _id: this get _bsontype(): 'ObjectId' // get [bsonType](): this['_bsontype'] // this does not work get id(): Uint8Array set id(value: Uint8Array) toHexString(): string toString(encoding?: 'hex' | 'base64'): string toJSON(): string equals(otherId: string | ObjectId | ObjectIdLike | undefined | null): boolean getTimestamp(): Date inspect(depth?: number, options?: unknown, inspect?: InspectFn): string } // Backend Code import {type Document, model, Schema, Types} from 'mongoose' interface IAccountDocument extends Document { _id: Types.ObjectId foo: string } const AccountSchema = new Schema({ foo: String, }) const AccountModel = model<IAccountDocument>('Account', AccountSchema) // This is an example and ultimately comes from the backend and is used in the backend but uses our own type const accountId = new Types.ObjectId() as ObjectId AccountModel.findOne({_id: accountId})This is the relevant error part that typescript gives me on the last line of code:
. Property [bsonType] is missing in type import("/src/ObjectId").ObjectId but required in type import("/node_modules/bson/bson").ObjectId. Overload 2 of 4,These are the type definitions in mongoose: https://raw.githubusercontent.com/Automattic/mongoose/refs/heads/master/types/types.d.ts#:~:text=class%20ObjectId%20extends%20mongodb.ObjectId https://raw.githubusercontent.com/Automattic/mongoose/refs/heads/master/types/augmentations.d.ts Which reference the types in bson: https://raw.githubusercontent.com/mongodb/js-bson/refs/heads/main/src/objectid.ts#:~:text=export%20class%20ObjectId%20extends%20BSONValue
I do not understand what is missing and how to provide it, or if this some symbol magic which explicitly prevents me from doing this.
