Skip to content

feat: disallow set on computed values without a setter - #4728

Merged
kubk merged 1 commit into
mobxjs:mainfrom
kubk:fix/readonly-computed-type
Oct 10, 2026
Merged

kubk merged 1 commit into
mobxjs:mainfrom
kubk:fix/readonly-computed-type

Conversation

@kubk

@kubk kubk commented Oct 10, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #3767

This will break code like const c: IComputedValue<number> = computed(() => x.get()) so strictly speaking it should have been major. Yet the code that breaks is code was already wrong. Calling .set() on a computed without setter always throws, so the new error points at a bug

@changeset-bot

changeset-bot Bot commented Oct 10, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: fa412ec

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
mobx Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@kubk
kubk requested a balanced review from Copilot October 10, 2026 03:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The breaking public type change is incorrectly classified as a minor release.

1 open finding
What changed in this PR

This PR improves type safety by distinguishing read-only and writable computed values.

Changes:

  • Adds IWritableComputedValue<T>.
  • Narrows computed() return types based on setter presence.
  • Adds TypeScript coverage and release notes.
File Description
packages/​mobx/​src/​mobx.ts Exports the writable interface.
packages/​mobx/​src/​core/​computedvalue.ts Splits read-only and writable interfaces.
packages/​mobx/​src/​api/​computed.ts Adds setter-aware overloads.
packages/​mobx/​__tests__/​base/​typescript-tests.ts Tests inferred setter availability.
.changeset/​readonly-computed-type.md Documents and versions the change.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@@ -0,0 +1,5 @@
---
"mobx": minor
@kubk

kubk commented Oct 10, 2026 •

Copy link
Copy Markdown
Collaborator Author

The major / minor concern is valid. Yet I double-checked that this MobX version doesn't break MST / mobx-keystone, both of the libraries use IComputedValue type. Even if there's a break - it's compile-only, it warns only on incorrect usage and it's easy to fix. I'd prefer reserving major for smth bigger

@kubk
kubk merged commit 457ea8f into mobxjs:main Oct 10, 2026
1 check passed
@github-actions github-actions Bot mentioned this pull request Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants