The actual issue here is, that I didn’t type collection correctly! The corrected function would look like this:
import { MongoClient } from "mongodb";
const dbUrl = "mongodb://localhost:27017/";
export const deleteDocumentWithId = (id: ObjectId) => {
return MongoClient.connect(dbUrl, (err, db) => {
if (err) {
throw err;
}
const dbo = db.db("my-db");
dbo.collection<DocumentType>("my-collection").deleteOne({ _id: id }, (err, obj) => {
// ^^^ Note this type-definition!
if (err) {
throw err;
}
db.close();
});
});
};
deleteDocumentWithId("EXISTING_ID");
It actually would have sufficed to type collection like this:
dbo.collection<{ _id: string }>("my-collection").deleteOne({ _id: id }, (err, obj) => {
I got this answer off of my own related questions on Reddit and Stackoverflow. I didn’t find any documentation saying, that collection is generic though. This might be an issue for more users.