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

UpdateFilter typings fail for typings including `number`

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.11.0
    • Component/s: TypeScript
    • 2
    • Not Needed
    • Needed
    • Hide

      We should add to our dot notation section in the typescript specific docs about using as const when building a key from variables, as long as the input types are correct, as const should make the final key type compatible with our schema assertions.

      [https://www.mongodb.com/docs/drivers/node/current/fundamentals/typescript/#type-safety-and-dot-notation|Our docs on dot notation]

      Show
      We should add to our dot notation section in the typescript specific docs about using as const when building a key from variables, as long as the input types are correct, as const should make the final key type compatible with our schema assertions. [https://www.mongodb.com/docs/drivers/node/current/fundamentals/typescript/#type-safety-and-dot-notation|Our docs on dot notation]

      What problem are you facing?

      I am experiencing typing issues when trying to update records in the database that include a number (array index). The typings show that it is supported, but when actually typing out the update it fails. The error is:

      Unable to find source-code formatter for language: txt. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      'string' and '`headquarters.$[${string}]`' index signatures are incompatible.
          Type 'string' is not assignable to type 'Headquarters'.(2322)
      

      Here is a basic example:

      Basic Gist:

      Unable to find source-code formatter for language: typescript. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      interface Client {
        _id: string;
        headquarters: Headquarters[];
      }
        
      interface Headquarters {
        country: string;
      }
        
      const hqCounter = 2;
      const client = { _id: 'acb123', };
      
      await clients.findOneAndUpdate(
        { _id: client._id },
        { $set: { [`headquarters.${hqCounter}.country`]: 'US' } }, // Type Error Here
      );
      

      What driver and relevant dependency versions are you using?

      This was experienced when we upgraded to v4, and currently we are on 4.11.0 and TypeScript 4.7.4.

      Steps to reproduce?

      I've put together a stackblitz that allows you to see the intellisense and highlights the type errors: https://stackblitz.com/edit/mongodb-updatefilter-issues?file=index.ts

      It appears from the error that it is expecting a different value type based on the key, so I believe there is something incorrect there. I've used my own "object to string path" types elsewhere in my project and it can handle this case just fine (I've added it to the bottom of the stackblitz example).

            Assignee:
            neal.beeken@mongodb.com Neal Beeken
            Reporter:
            jim@jimbuck.io Jim Buck
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: