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

TypeScript: wrong findOne overload often picked

    • Type: Icon: Improvement Improvement
    • Resolution: Works as Designed
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 4.4.1
    • Component/s: None
    • Labels:
    • Not Needed

      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:
            staffan.eketorp@gmail.com Staffan Eketorp
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: