If you know how to fix the issue, make a pull request instead.
@types/xxxx package and had problems.Definitions by: in index.d.ts) so they can respond.Mongoose now allows loading behavior from an ES6 class into the schema using MySchema.loadClass(MyClass);
This seems to work, but within the class the schema's properties obviously cannot be found.
class MyClass {
public foo() { console.log(this.bar); } // this.bar cannot be found, intellisense complains, warnings, etc.
}
I'm not sure how to deal with this other than allowing the warnings. If this cannot be addressed, at least maybe we can get a new section for it in the README.md explaining how to use it?
The solution is to annotate each function in the class with a this parameter, which tells typescript what this refers to when the function is called.
class UserClass {
getFullName(this: IUser) {
return `${this.firstName} ${this.lastName}`;
}
}
@jendrikw Thanks that works!
I actually discovered that if I change from class MyClass { } to class MyClass extends mongoose.Model { } then the problem goes away. But your solution is more accurate and I get full intellisense as well!
I guess this is not a bug. But it should be in the docs, it's so useful.
For the record, your code typechecks because Model has a construct signature of new(doc?: any): T; with T defaulting to any. The result is that you can access any property of this.