I am mocking the User
and need to implement static method findOne
which is static so I do not need to extensiate User
in my calling class:
export class User implements IUser {
constructor(public name: string, public password: string) {
this.name = 'n';
this.password = 'p';
}
static findOne(login: any, next:Function) {
if(this.name === login.name) //this points to function not to user
//code
return this; //this points to function not to user
}
}
But I can't access this
from static function findOne
is there a ways of doning it in typescript?
It's not possible. You can't get an instance property from a static method because there is only one static object and an unknown number of instance objects.
You can, however, access static members from an instance. This will probably be useful for you:
try this