Skip to content

Commit c18ef31

Browse files
author
omer
committed
types: support readonly array in query.select
1 parent 133f2ca commit c18ef31

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

test/types/queries.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
UpdateQuery,
1414
UpdateQueryKnownOnly,
1515
QuerySelector,
16+
InferRawDocType,
1617
InferSchemaType,
1718
ProjectionFields,
1819
QueryOptions,
@@ -710,3 +711,17 @@ function gh14841() {
710711
$expr: { $lt: [{ $size: '$owners' }, 10] }
711712
};
712713
}
714+
715+
async function gh15526() {
716+
const userSchemaDefinition = { name: String, age: Number } as const;
717+
const UserModel = model('User', new Schema(userSchemaDefinition));
718+
type UserType = InferRawDocType<typeof userSchemaDefinition>;
719+
720+
const selection = ['name'] as const satisfies readonly (keyof UserType)[];
721+
722+
const u1 = await UserModel.findOne()
723+
.select<Pick<UserType, (typeof selection)[number]>>(selection)
724+
.orFail();
725+
expectType<string | undefined | null>(u1.name);
726+
expectError(u1.age);
727+
}

types/query.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ declare module 'mongoose' {
758758

759759
/** Specifies which document fields to include or exclude (also known as the query "projection") */
760760
select<RawDocTypeOverride extends { [P in keyof RawDocType]?: any } = {}>(
761-
arg: string | string[] | Record<string, number | boolean | string | object>
761+
arg: string | readonly string[] | Record<string, number | boolean | string | object>
762762
): QueryWithHelpers<
763763
IfEquals<
764764
RawDocTypeOverride,

0 commit comments

Comments
 (0)