'Mongoose pre save gives me red lines?

In the picture below red lines indicates error. I have this code inside a typescript file. @types/mongoose is already installed... The code works fine. The error when hovering over the red line: "This implicitly has type 'any' because it has not type annotation"

enter image description here



Solution 1:[1]

You need to type this explicitly:

}).pre("save", function(this: Type, next) {

where Type is the type this should be.

The caller side does not change - the first this argument is removed during compilation.

References:

Solution 2:[2]

}).pre< Type >("save", function(next) {

this is another option: Type is the type this should be

Solution 3:[3]

 new Schema<SchemaType, Model<SchemaType>>({}).pre<SchemaType>("save",function(next){
    // .... your logic
    next()
    })

Model - comes from "mongoose"

for more information follow the mongoose documentation https://mongoosejs.com/docs/typescript/schemas.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zerkms
Solution 2 Bary Levy
Solution 3 ranjit sahoo