Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(typescript): explain that virtuals inferred from schema only show up on Model, not raw document type #12860

Merged
merged 1 commit into from Jan 6, 2023

Conversation

vkarpov15
Copy link
Collaborator

Fix #12684

Summary

Documentation related to where virtuals show up with automatically inferred schema types, and why they show up where they do. Also how to get the hydrated doc type. Because we didn't have a good link to answer @Jokero 's questions in #12684

Examples

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Jokero
Copy link
Contributor

Jokero commented Jan 4, 2023

The problem with type UserDocument = ReturnType<(typeof UserModel)['hydrate']>; is that it returns Document type.
While it can be useful with normal (hydrated) queries, it won't work with lean option.

My current workaround is to manually extend the type returned by InferSchemaType:

type Point = InferSchemaType<typeof pointSchema> & {
    // virtual fields
    id: string;
    displayName: string;
}

And I can use this type with both normal (hydrated) and lean queries. This is what I wanted to report in #12684

Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! 👍

@vkarpov15
Copy link
Collaborator Author

@Jokero I presume you're using mongoose-lean-virtuals?

@vkarpov15 vkarpov15 merged commit b9f2127 into master Jan 6, 2023
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-12684 branch January 6, 2023 16:57
@Jokero
Copy link
Contributor

Jokero commented Jan 6, 2023

@vkarpov15 yeah, I'm using it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

InferSchemaType does not infer types for virtual fields from schema
4 participants