Skip to content

Commit 1bfcdf6

Browse files
authored
Merge pull request #14047 from Automattic/vkarpov15/gh-14026
types(model+query): unpack arrays in distinct return type
2 parents 659f000 + 18ddd13 commit 1bfcdf6

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

test/types/models.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,3 +771,21 @@ function gh13897() {
771771
expectType<Date>(doc.createdAt);
772772
expectError(new Document<IDocument>({ name: 'foo' }));
773773
}
774+
775+
async function gh14026() {
776+
interface Foo {
777+
bar: string[];
778+
}
779+
780+
const FooModel = mongoose.model<Foo>('Foo', new mongoose.Schema<Foo>({ bar: [String] }));
781+
782+
const distinctBar = await FooModel.distinct('bar');
783+
expectType<string[]>(distinctBar);
784+
785+
const TestModel = mongoose.model(
786+
'Test',
787+
new mongoose.Schema({ bar: [String] })
788+
);
789+
790+
expectType<string[]>(await TestModel.distinct('bar'));
791+
}

types/models.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ declare module 'mongoose' {
481481
field: DocKey,
482482
filter?: FilterQuery<TRawDocType>
483483
): QueryWithHelpers<
484-
Array<DocKey extends keyof TRawDocType ? TRawDocType[DocKey] : ResultType>,
484+
Array<DocKey extends keyof TRawDocType ? Unpacked<TRawDocType[DocKey]> : ResultType>,
485485
THydratedDocumentType,
486486
TQueryHelpers,
487487
TRawDocType,

types/query.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ declare module 'mongoose' {
320320
distinct<DocKey extends string, ResultType = unknown>(
321321
field: DocKey,
322322
filter?: FilterQuery<DocType>
323-
): QueryWithHelpers<Array<DocKey extends keyof DocType ? DocType[DocKey] : ResultType>, DocType, THelpers, RawDocType, 'distinct'>;
323+
): QueryWithHelpers<Array<DocKey extends keyof DocType ? Unpacked<DocType[DocKey]> : ResultType>, DocType, THelpers, RawDocType, 'distinct'>;
324324

325325
/** Specifies a `$elemMatch` query condition. When called with one argument, the most recent path passed to `where()` is used. */
326326
elemMatch<K = string>(path: K, val: any): this;

0 commit comments

Comments
 (0)