-
Notifications
You must be signed in to change notification settings - Fork 12.8k
'this' reference from static method should be possible #6331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
this
reference from static method should be possible
It is legal to reference |
Oh sorry, you are right. But it produces that error if you do it from an static arrow function and I guessed it would happen from a static method too. Anyway, that is incorrect, isn't it? class Model {
public static where = (options: any) => doWhereQuery(Reflect.getMetadata('table', this.prototype));
}
|
What's the intent with writing it as an arrow function and referencing If you want If you want |
I forgot that arrow functions doesn't have own this, so this issue should be closed. Sorry for my ignorance. |
In JavaScript I am able to reference
this
from object methods, in order to access properties from the same object that stores my method, for example the prototype of the object.Suppose I'm writing an ORM and I have a class
Model
and I want to implement an static methodwhere
that does a query in the DB for the corresponding table to thatModel
. I would write it like this:But when I do so I get:
error TS2334: 'this' cannot be referenced in a static property initializer.
I understand that maybe
this
is not the correct keyword to reference the "class", but that is the way it works in JavaScript and I think that this should be possible Otherwise, there is no trivial way to implement that functionality.The text was updated successfully, but these errors were encountered: