Typescript error is driving me crazy!

Dear Mongoers,
I need help with some weird TS error I get only when using findOneAndUpdate in Node JS. This sometimes works, sometimes not! :frowning:

The situation is about event management. If a user joins the event, they can either book a spot, or join the waiting list:

I am using Node JS with TS. TS sometimes complains about this:

const list = numberJoined < capacity ? "joined" : "waitingList";
const counterList = list === "joined" ? "waitingList" : "joined";

const event = await db?.collection("events-jordan").findOneAndUpdate(
    { _id: new ObjectId(eventId) },
    {
      $addToSet: {
        [list]: userId,
      },
      $pull: {
        [counterList]: userId,
      },
    },
    { returnDocument: "after" }
  )

I get the following error from TS which I am unable to solve :frowning:

No overload matches this call.
Overload 2 of 4, ‘(filter: Filter, update: UpdateFilter, options: FindOneAndUpdateOptions): Promise<…> | undefined’, gave the following error.
Type ‘{ [x: string]: string; }’ is not assignable to type ‘SetFields’.
Type ‘{ [x: string]: string; }’ is not assignable to type ‘NotAcceptedFields<Document, readonly any | undefined>’.
‘string’ index signatures are incompatible.
Type ‘string’ is not assignable to type ‘undefined’.
Overload 2 of 4, ‘(filter: Filter, update: UpdateFilter, options: FindOneAndUpdateOptions): Promise<…> | undefined’, gave the following error.
Type ‘{ [x: string]: string; }’ is not assignable to type ‘PullOperator’.
Type ‘{ [x: string]: string; }’ is not assignable to type ‘{ readonly [x: string]: Partial | { [x: string]: FilterOperators | undefined; } | FilterOperators | undefined; }’.
‘string’ index signatures are incompatible.
Type ‘string’ is not assignable to type ‘Partial | { [x: string]: FilterOperators | undefined; } | FilterOperators | undefined’.ts(2769)
mongodb.d.ts(5770, 5): The expected type comes from property ‘$addToSet’ which is declared here on type ‘UpdateFilter’
mongodb.d.ts(5772, 5): The expected type comes from property ‘$pull’ which is declared here on type ‘UpdateFilter’

Any help is very much appreciated.
Thank you :slight_smile:

The above seems to indicate that your variable userId is the culprit. How is it declared?