Skip to content

Commit

Permalink
fix(types): infer virtuals in query results
Browse files Browse the repository at this point in the history
Fix #12702
Re: #11908
  • Loading branch information
vkarpov15 committed Nov 26, 2022
1 parent dc42cb2 commit 6b6b7db
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 4 additions & 1 deletion test/types/virtuals.test.ts
Expand Up @@ -87,7 +87,7 @@ function gh11543() {
expectType<PetVirtuals>(personSchema.virtuals);
}

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

expectType<FlatRecord<AutoTypedSchemaType & VirtualsType >>({} as InferredDocType);

const doc2 = await TestModel.findOne().orFail();
expectType<string>(doc2.domain);
}
8 changes: 7 additions & 1 deletion types/index.d.ts
Expand Up @@ -78,7 +78,13 @@ declare module 'mongoose' {
schema?: TSchema,
collection?: string,
options?: CompileModelOptions
): Model<InferSchemaType<TSchema>, ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>, ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>, {}, TSchema> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
): Model<
InferSchemaType<TSchema>,
ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
ObtainSchemaGeneric<TSchema, 'TVirtuals'>,
TSchema
> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;

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

Expand Down

0 comments on commit 6b6b7db

Please sign in to comment.