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

Investigate NODE-4095 - TypeScript: wrong findOne overload often picked

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
    • Not Needed

      NODE-4095 Description
      See https://www.mongodb.com/community/forums/t/mongodb-4-1-4-with-typescript-findone-syntax-error-type-void-is-not-assignable-to-type-dbuser-undefined-ts-2322/131533

      The reason for this is when you write some generic code, e.g. something like

      function findSomeThings<TSchema>(collection: Collection<TSchema>):Promise<TSchema> {
       const filter = {} as Filter<TSchema>;
       return collection.findOne(filter);
       }
      

      the overload with Callback is picked. This might be unintuitive but it's because Filter overlaps with Callback as types as Filter can just be Partial<T> with equals Partial<any> = any in this code. Thus I suggest reordering those overloads. The other scenario seems unlikely to kick in, as something typed to Callback<WithId<T>> would still resolve to the right overload even if the Filter one comes first

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

              Created:
              Updated:
              Resolved: