Would it be possible for the MongoDB Node.js driver to give a typescript warning if filtering on field that does not exists?

Let’s say I have documents:

  "foo": number,
  "bar": string

The Node.js driver correctly warns me, if I try to do a findOne on foo with a string.

However, I get no warnings here:

col.findOne({unknownField: 123})

Could the driver be improved to indicate a typescript error here (potentially with a strict option)?

Hi @Alex_Bjorlig

Node JS is not a typed language, so it won’t warn you about this type of thing. One way to get this behavior is to use TypeScript with strict mode and create the types for the documents or entities. In the following example, it will warn you for missing fields of fields that don’t exist in the type.


type User = {name: string, age: number}

const coll = db.collection<User>("users");

I would not be so sure about that, but maybe I miss something here?

Link to repo here.

@Alex_Bjorlig per your repo you are in fact using TypeScript, and to your question directly, per our driver documentation regarding TypeScript usage:

Keys Not in Type Parameter Receive any Type

Keys not listed in your specified type parameter receive the any type. The following code snippet demonstrates this behavior:

interface User {
  email: string;

const database = client.db("<your database>");
const myColl = db.collection<User>("<your collection>");
myColl.find({ age: "Accepts any type!" });

Hi @alexbevi - sure, thanks for pointing that out.

A feature request from our team would then be an option, to not allow keys not in type.

We had a production error, that could have been prevented if typescript had found the error.

Maybe it could be a setting in the driver - a strict type option?