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

Typescript errors on _id after update to 4.2.1

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 4.3.0
    • Affects Version/s: None
    • Component/s: TypeScript
    • 5
    • Needed

      What problem are you facing?

      After updating to 4.2.1 from 4.1.4 we are having issues managing types where the _id is a string instead of ObjectId.

      If `_id` is not required (i.e. allow undefined) InferIdType fails and infers the id to be ObjectId.

      If we make `_id` required, InferIdType infers the type correctly, but then insertOne and similar functions require the `_id` to be present, even if we defined the proper pkFactory on the client

      What driver and relevant dependency versions are you using?

      We are using nodes driver version 4.2.1

      Steps to reproduce?

       

       

      import { randomUUID } from 'crypto' import { MongoClient, InferIdType } from 'mongodb'
       
      interface TestDocument1 {
       _id: string
       name: string
      }
      interface TestDocument2 {
       _id?: string
       name: string
      }
      type id1 = InferIdType<TestDocument1> // string
      type id2 = InferIdType<TestDocument2> // object id
      const client = await MongoClient.connect(url, {
       pkFactory: {
       createPk: randomUUID
       }
       })
      client.db().collection<TestDocument1>('test_collection').insertOne({ name: 'test1'}) // compilation error
       client.db().collection<TestDocument2>('test_collection').insertOne({ name: 'test2'}) // compilation error
      

       

            Assignee:
            bailey.pearson@mongodb.com Bailey Pearson
            Reporter:
            chiodi84@gmail.com Paolo Chiodi
            Daria Pardue
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: