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

Investigate NODE-4530 - MatchKeysAndValues type incorrectly infers types for nested objects with variables

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None
    • 2
    • Not Needed

      NODE-4530 Description

      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).

            Assignee:
            bailey.pearson@mongodb.com Bailey Pearson
            Reporter:
            dbeng-pm-bot PM Bot
            Bailey Pearson
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: