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

Add types for global instalation #219

Open
SkyaTura opened this issue Jul 22, 2024 · 0 comments
Open

Add types for global instalation #219

SkyaTura opened this issue Jul 22, 2024 · 0 comments

Comments

@SkyaTura
Copy link
Contributor

Is your feature request related to a problem? Please describe.
If you install the plugin globally mongoose.plugin(MongoosePaginateV2), TypeScript won't understand that model.paginate is a valid function.

Describe the solution you'd like
Create a global type definition on Model interface to paginate.

  export interface Model<
    TRawDocType,
    TQueryHelpers = NonNullable<unknown>,
    TInstanceMethods = NonNullable<unknown>,
    TVirtuals = NonNullable<unknown>,
    THydratedDocumentType = HydratedDocument<
      TRawDocType,
      TVirtuals & TInstanceMethods,
      TQueryHelpers
    >,
    TSchema = any,
  > extends NodeJS.EventEmitter,
    AcceptsDiscriminator,
    IndexManager,
    SessionStarter {
    paginate<O extends PaginateOptions>(
      query?: FilterQuery<TRawDocType>,
      options?: O,
      callback?: (
        err: any,
        result: PaginateResult<PaginateDocument<TRawDocType, TInstanceMethods, O>>
      ) => void
    ): Promise<PaginateResult<PaginateDocument<TRawDocType, TInstanceMethods, O>>>
    paginate<UserType = TRawDocType, O extends PaginateOptions = PaginateOptions>(
      query?: FilterQuery<TRawDocType>,
      options?: O,
      callback?: (
        err: any,
        result: PaginateResult<PaginateDocument<UserType, TInstanceMethods, O>>
      ) => void
    ): Promise<PaginateResult<PaginateDocument<UserType, TInstanceMethods, O>>>
    paginate<UserType = TRawDocType>(
      query?: FilterQuery<TRawDocType>,
      options?: PaginateOptions,
      callback?: (
        err: any,
        result: PaginateResult<PaginateDocument<UserType, TInstanceMethods, PaginateOptions>>
      ) => void
    ): Promise<PaginateResult<PaginateDocument<UserType, TInstanceMethods, PaginateOptions>>>
  }

There is one catch though, this will type all models even when not using paginate.

Probably there as a nice way to toggle this, but I can't think on one yet.

Describe alternatives you've considered
Copy and paste this code on a .d.ts file on the project.
An alternative and simple solution for that would be include this on the README.

Additional context
That's all.

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

No branches or pull requests

1 participant