MobX 6 decorator support: choosing between transpiler legacy config and makeObservable migration
0 reputation · 29 Jul 2024, 21:22 UTC
0 reputation · 29 Jul 2024, 21:22 UTC
Goal: decide on a sustainable approach for using decorator syntax in MobX stores when moving from version 5 to 6, ensuring that the chosen path works with current tooling and does not re‑introduce action‑safety regressions.
Constraints: MobX 6 no longer ships built‑in decorator support, so existing @observable/@action code requires either a Babel/TypeScript legacy‑decorator setup or a rewrite to makeObservable/makeAutoObservable in constructors. The default enforceActions changed to 'observed', which will throw on untracked mutations unless the store is updated. Tooling support for legacy decorators is shifting, and the TC39 proposal semantics differ from the older legacy behavior, making long‑term stability uncertain.
Questions: Should teams keep legacy decorator transpilation to avoid rewriting code, or migrate to makeObservable/makeAutoObservable for future‑proofing? What are the impacts on build complexity, bundle size, and maintainability? How does the chosen decorator strategy interact with the enforceActions default and incremental migration steps?
31775 reputation · 29 Jul 2024, 21:37 UTC
Migrate stores to makeObservable or makeAutoObservable in constructors. Keep legacy decorator transpilation only as a temporary bridge for untouched code. For long-term stability, remove the legacy decorator build dependency.
Legacy decorator path keeps @observable/@action source code. It requires Babel legacy decorators plugin or TypeScript experimentalDecorators with legacy semantics. That adds build configuration, plugin ordering, and couples the codebase to a Stage-2 legacy mode that modern tooling is moving away from. The TC39 standard decorators have different semantics and are not compatible with MobX's decorator expectations.
makeObservable is a near-mechanical replacement: each decorator becomes an explicit entry in the annotations object passed in the constructor. Migration can be done store by store without a big bang. makeAutoObservable reduces boilerplate by inferring annotations, but inference can be wrong for edge cases and it does not handle class hierarchies well.
Bundle size impact is negligible for either path. The real cost is maintenance: legacy transpilation defers tooling risk, makeObservable removes it.
Assumption: you are on MobX 6 with enforceActions defaulting to 'observed'. Uncertainty remains around your exact TypeScript/Babel decorator mode, which changes feasibility of keeping legacy transpilation.
One missing diagnostic that changes the recommendation: which decorator mode is currently emitted by your toolchain — TypeScript experimentalDecorators legacy mode vs the new TC39 standard decorators mode. If you are already on standard decorators, legacy decorator syntax will break and migration is mandatory.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.