I'm encountering a peculiar problem with the MobX annotations, where a method marked with @action
seems to be missing from the resulting object.
Let's consider the following TypeScript code snippet as a basic example:
export class Car {
@observable
public wheels: number = 4;
@action
public selfDestruct() {
this.wheels = 0;
}
}
Upon calling the method like this:
const car = new Car();
car.selfDestruct();
An error is thrown:
Uncaught TypeError: car.selfDestruct is not a function
When evaluating car.selfDestruct()
in the console, it returns undefined
.
However, using the action as a function seems to resolve the issue:
export class Car {
@observable
public wheels: number = 4;
public selfDestruct = action(
() => this.wheels = 0
);
}
const car = new Car();
car.selfDestruct(); // works perfectly
Just to clarify, I am working with MobX 5.5.2 in conjunction with TypeScript 3.1.1. The compilation process is managed by ParcelJS 1.10.1.