Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-4530

MatchKeysAndValues type incorrectly infers types for nested objects with variables

      What problem are you facing? 

      I have an interface in which one of the properties is a Record of strings mapped to an object with two properties. When trying to change one of those properties through an update operation I used the following update query: `{ $set: { [`categories.${category}.name`]: name }}` and got the following error:

      'string' and '`categories.${string}.name`' index signatures are incompatible.
      Type 'string' is not assignable to type '{ id: string; name: string; }'. ts(2322)

      This is because MongoDB thinks the correct type of the properties in the object is the object itself when this is not true. This is what the types for $set are resolved to:

      And this is how that property in my interface is defined:

      What driver and relevant dependency versions are you using?

      Driver version 4.8.1 on Node.js v18.7.0 and TypeScript 4.7.4

      Steps to reproduce?

      Not exactly sure if this will do it but this is what I have

      1. Define an interface with a property of type `Record<string, YourInterface>` where `YourInterface` is an object with at least one property
      2. Assign that interface to a collection and try to do an update operation where you change one of the properties in your Record, as shown above.
      3. Observe the TypeScript error (the query should complete on the server side with no issues).

        1. image-2022-08-11-17-48-32-466.png
          12 kB
          Rodrigo Leitão
        2. image-2022-08-11-17-49-04-292.png
          4 kB
          Rodrigo Leitão

            Assignee:
            Unassigned Unassigned
            Reporter:
            rodrigoleitao25@gmail.com Rodrigo Leitão
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: