Skip to content

Add types for global instalation #219

Open
@SkyaTura

Description

@SkyaTura

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions