mobx icon indicating copy to clipboard operation
mobx copied to clipboard

The value returned from `computed(() => ...)` shouldn't have a `set(...)` method

Open Nokel81 opened this issue 9 months ago • 8 comments

Intended outcome:

Have strict and complete type safety to help with refactoring.

Actual outcome:

The return type of computed() and observable.box() both have set() and get() methods, but the return type of computed() doesn't have a set() method so a type error is thrown at run time.

How to reproduce the issue:

const myObservableValue = observable.box(10);
const myComputedValue = computed(() => myObservableValue.get() + 1);

/**
 * Uncaught:
 * Error: [MobX] [ComputedValue 'ComputedValue@2'] It is not possible to assign a new value to a computed value.
 *    at die (/node_modules/mobx/dist/mobx.cjs.development.js:89:11)
 *    at ComputedValue.set (/node_modules/mobx/dist/mobx.cjs.development.js:1448:7)
 */
myComputedValue.set(12);

Versions

mobx -> 6.10.9

Nokel81 avatar Sep 26 '23 13:09 Nokel81