Skip to content

Commit 4d51e25

Browse files
authored
Merge pull request #12727 from Automattic/vkarpov15/gh-12702
fix(types): infer virtuals in query results
2 parents dc42cb2 + 6b6b7db commit 4d51e25

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

test/types/virtuals.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function gh11543() {
8787
expectType<PetVirtuals>(personSchema.virtuals);
8888
}
8989

90-
function autoTypedVirtuals() {
90+
async function autoTypedVirtuals() {
9191
type AutoTypedSchemaType = InferSchemaType<typeof testSchema>;
9292
type VirtualsType = { domain: string };
9393
type InferredDocType = FlatRecord<AutoTypedSchemaType & ObtainSchemaGeneric<typeof testSchema, 'TVirtuals'>>;
@@ -119,4 +119,7 @@ function autoTypedVirtuals() {
119119
expectType<string>(doc.domain);
120120

121121
expectType<FlatRecord<AutoTypedSchemaType & VirtualsType >>({} as InferredDocType);
122+
123+
const doc2 = await TestModel.findOne().orFail();
124+
expectType<string>(doc2.domain);
122125
}

types/index.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ declare module 'mongoose' {
7878
schema?: TSchema,
7979
collection?: string,
8080
options?: CompileModelOptions
81-
): Model<InferSchemaType<TSchema>, ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>, ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>, {}, TSchema> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
81+
): Model<
82+
InferSchemaType<TSchema>,
83+
ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
84+
ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
85+
ObtainSchemaGeneric<TSchema, 'TVirtuals'>,
86+
TSchema
87+
> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
8288

8389
export function model<T>(name: string, schema?: Schema<T, any, any> | Schema<T & Document, any, any>, collection?: string, options?: CompileModelOptions): Model<T>;
8490

0 commit comments

Comments
 (0)