
As what you can see, the "replace" method is not support by ts. Maybe I should use IObservableArray instead of Array, but another problem occurred.

There are a few solutions. but yes you should use IObservableArray so you make sure you have and use the right methods.
I used this work around:
this.files = [] as any;
or a more cast type once solution:
function MyArray<T>(): IObservableArray<T> {
return [] as any; // disable eslint once here.
}
this.files = MyArray();
This issue was discussed before on github: https://github.com/mobxjs/mobx/issues/669
The solution provided is to set it as readonly property once:
class AppModel {
readonly files = observable<IFile>([])
}
this is the shortest typed solution that also makes sure you wont set this.files again mistakenly (and losing all your observers).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With