Within my code, I have a function with the following definition:
export async function removeUser(deleteUserId: Types.ObjectId)
There was an instance where I mistakenly called this function and passed a Mongoose object's id
parameter, which is a string. This led to a runtime exception later in the code when I attempted to utilize the .equals()
method that is present on an ObjectId but not on a string:
await removeUser(deleteUser.id);
Upon correcting this mistake by passing the ObjectId instead, everything functioned correctly:
await removeUser(deleteUser._id);
I am curious as to why TypeScript did not provide a compile time error when the argument was specified as an ObjectId but a string was being passed instead?
Edit: I have observed that the .id
is declared as any
, not specifically a string
. Could this be a factor influencing the issue?