mongoosejs / mongoose-lean-virtuals

Attach virtuals to the results of mongoose lean() queries
Apache License 2.0
45 stars 24 forks source link

Not working with Typescript #58

Closed SinBirb closed 2 years ago

SinBirb commented 2 years ago

When using with

import mongooseLeanVirtuals from "mongoose-lean-virtuals";

it gives the same error as the mongoose-lean-getters plugin: https://github.com/mongoosejs/mongoose-lean-getters/issues/11

/usr/current/app/node_modules/mongoose/lib/schema.js:1406
    throw new Error('First param to `schema.plugin()` must be a function, '

Is there some other way to use this plugin?

iammola commented 2 years ago

I had this issue as well and solved it by adding this code into another types declaration file. It's temporary, but it works.

declare module "mongoose-lean-virtuals" {
  import { Schema } from "mongoose";
  export default function mongooseLeanVirtuals(schema: Schema): void;
}
MarcusElevait commented 2 years ago

we use it like that:

import * as leanVirtualsPlugin from 'mongoose-lean-virtuals';

Schema.plugin(leanVirtualsPlugin);

and it works