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

Get Document type with QueryHelpers and InstanceMethods #12319

Closed
1 task done
kot-shrodingera opened this issue Aug 23, 2022 · 3 comments · Fixed by #12464
Closed
1 task done

Get Document type with QueryHelpers and InstanceMethods #12319

kot-shrodingera opened this issue Aug 23, 2022 · 3 comments · Fixed by #12464
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@kot-shrodingera
Copy link

Prerequisites

  • I have written a descriptive issue title

Mongoose version

6.5.2

Node.js version

17.3

MongoDB version

5.0

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

11

Issue

How can I get infered Document type in typescript? So I could use it as argument type in DTO or enywhere else. For example I have this:

const projectSchema = new mongoose.Schema(
  {
    name: {
      type: String,
      required: true,
    },
  },
  {
    methods: {
      async doSomething() {
        // ...
      },
    },
  }
);

const ProjectModel = mongoose.model('Project', projectSchema);

Basically, I want a return type of await ProjectModel.findOne(). Closest I got is:

export type ProjectDocument = mongoose.HydratedDocument<
  mongoose.InferSchemaType<typeof projectSchema>,
  mongoose.ObtainSchemaGeneric<typeof projectSchema, 'TQueryHelpers'>,
  mongoose.ObtainSchemaGeneric<typeof projectSchema, 'TInstanceMethods'>
>;

But it looks bulky. Is there another way?

@kot-shrodingera kot-shrodingera added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Aug 23, 2022
@kot-shrodingera kot-shrodingera changed the title Get Document type Get Document type with QueryHelpers and InstanceMethods Aug 23, 2022
@github-actions
Copy link

github-actions bot commented Sep 7, 2022

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the Stale label Sep 7, 2022
@github-actions
Copy link

This issue was closed because it has been inactive for 19 days since being marked as stale.

@vkarpov15 vkarpov15 reopened this Sep 14, 2022
@vkarpov15 vkarpov15 added this to the 6.5.7 milestone Sep 14, 2022
@vkarpov15 vkarpov15 added typescript Types or Types-test related issue / Pull Request and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted Stale labels Sep 14, 2022
vkarpov15 added a commit that referenced this issue Sep 22, 2022
vkarpov15 added a commit that referenced this issue Sep 26, 2022
fix(types): add HydratedDocumentFromSchema to make it easier to pull inferred hydrated doc type
@kot-shrodingera
Copy link
Author

I guess it's not quite right. Methods are infered, but virtuals aren't
Maybe it must be

  export type HydratedDocumentFromSchema<TSchema extends Schema> = HydratedDocument<
  InferSchemaType<TSchema>,
  ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
  ObtainSchemaGeneric<TSchema, 'TVirtuals'>
  >;

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
2 participants