You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Consider this implmentation of a ORM-like Model class:
classModel{publicstatictableName: string;publicstaticfindById(id: number): this {// error: a this type is only available in a non-static member of a class or interface constrows=db.query(`SELECT * FROM ${this.tableName} WHERE id = ?`,[id]);constinstance=newthis();for(constcolumnofrows[0]){instance[column]=rows[0][column];}returninstance;}}classUserextendsModel{publicstatictableName='users';publicusername: string;}constuser=User.findById(1);// user instanceof User
Currently, this is not possible, because TypeScript does not allow this in static members. There is no reason for this limitation: this inside a static method is simply the class in JavaScript (aka typeof Model or typeof User, depending on what class this is called on).
The text was updated successfully, but these errors were encountered:
Consider this implmentation of a ORM-like Model class:
Currently, this is not possible, because TypeScript does not allow
this
in static members. There is no reason for this limitation:this
inside a static method is simply the class in JavaScript (akatypeof Model
ortypeof User
, depending on what class this is called on).The text was updated successfully, but these errors were encountered: