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

distinct() has wrong formal return type

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 4.0.0
    • Affects Version/s: 4.0.0
    • Component/s: None
    • Labels:
    • Not Needed

      The DistinctOperation is defined as follows: 

      export class DistinctOperation extends CommandOperation<Document[]> { ... }
      

      As a consequence, the formal return type of all distinct() methods (e.g. on Collection)) is Document[]

      distinct(key: string): Promise<Document[]>;
      distinct(key: string, callback: Callback<Document[]>): void;
      ... (more overloads) ...
      

      But this is wrong for fields with basic types; here, the actual return value is an array of the basic type (e.g string[]) instead. A direct type cast from Document[] to an array of basic type is not possible:

      // firstNames is typed as Document[], however, the actual type is a string[]
      const firstNameDocs = await db.collection('person').distinct('firstName');// this type cast will fail at compile time:
      const firstNames = firstNameDocs as string[];
      // instead we need an intermediate type cast to unknown:const firstNames = firstNameDocs as unknown as string[];

            Assignee:
            daria.pardue@mongodb.com Daria Pardue
            Reporter:
            mario.winterer@gmail.com Mario Winterer
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: