Linting equality checks of MongoDB ObjectIDs

Viewed 32

I've been looking around for a nice way to prevent writing objectId1 === objectId2 when equality-checking MongoDB ObjectIds. Would be nice to have a linting rule (or TypeScript solution) to enforce this.

The problem:

const theObjectIdAsString = "6228ab5bedddb4fb156e1f73";

const objectId = new ObjectId(theObjectIdAsString)
const sameObjectId = new ObjectId(theObjectIdAsString)

// works as expected
objectId.equals(sameObjectId) // true
objectId.toHexString() === sameObjectId.toHexString() // true

// the oh-so-easy-to-make-mistake
objectId === sameObjectId // false

Any suggestions?

0 Answers
Related