mongoose virtuals with typescript error - The containing arrow function captures the global value of 'this' which implicitly has type 'any'
Arrow functions by definition capture this
from the declaration context. Since you are defining the function directly in the module this will be the global object.
You want to a regular function. A regular function, when called on an object will have this
passed into it as the actual object it was called on.
You also need to add a type annotation for this
(depeding on your strict settings but you mention you tried a regular function so this could be the issue), either this: any
or something more specific to tell typescript you did not mistakenly access this
UserSchema.virtual("username").get(function(this: { firstName: string, lastName: string}) {
return this.firstName + " " + this.lastName ;
}) ;