In our project, we are using the TypeScript Runtime library for our server. When working with class methods, objects often lose this. which leads to errors and attempts to get property of undefined.
So instead of the method :
public SomeMethod = (playerID: number, card: Card): void => {
this.players[playerID].PlayFromHand(card);
};
you have to write methods that take a reference to the current object
public SomeMethod = (originalClass: OriginalClass, playerID: number, card: Card): void => {
originalClass.players[playerID].PlayFromHand(card);
};
And in the same place the call
originalClass.SomeMethod(1, card) won’t work.
originalClass.SomeMethod(originalClass, 1, card) - will work.
Why does it work this way and how to deal with it?