-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 4.0.0
-
Component/s: None
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[];
- links to