Skip to content

What do MobX 7 annotation maps need from TypeScript? #227

Closed

Description

@jtklein-agents

Question

How does MobX 7 type makeObservable annotation maps in TypeScript: subclass overrides, non-public members (the AdditionalKeys generic), computed getters, and flow? Which of those shapes does the page store use, and does useDefineForClassFields: false in tsconfig.json matter?

Activity

  1. added
    wayfinder:research
    on Nov 16, 2026
  2. jtklein-agents commented on Nov 16, 2026

    @jtklein-agents
    OwnerAuthor

    Resolution

    Findings: https://github.com/jtklein-agents/raxmlGUI/blob/research/mobx7-typescript/docs/research/mobx7-typescript.md

    Premise correction. tsconfig.json on master already has useDefineForClassFields: true and no experimentalDecorators (changed in AntonelliLab#504). That is the setting MobX documents as required for TypeScript.

    Typing of annotation maps (MobX 7.1.7, TypeScript 5.9.3)

    • makeObservable<T, AdditionalKeys = never>(target, annotations: AnnotationsMap<T, NoInfer<AdditionalKeys>>, options?). Map keys are keyof T minus toString (public members only), all optional.
    • Private/protected members need the explicit generic, makeObservable<Store, "secret">(this, {...}); otherwise TS2353: Object literal may only specify known properties, and 'secret' does not exist in type 'AnnotationsMap<this, never>'. A key named in AdditionalKeys must then be annotated.
    • The annotation kind is not type-checked against the member, and override is a plain Annotation. All subclassing rules (override only prototype action/computed/flow/actionBound; no re-annotation except override; each class annotates its own members; no redeclaring annotated fields) are enforced at runtime in development builds, not by the types.
    • computed getters need nothing: the type is the getter's return type. A flow generator method stays typed as a Generator; flowResult(...) casts it to CancellablePromise<R>. actionBound, observableRef, observableShallow leave the member's declared type unchanged.
    • MobX 7 versus 6: the namespaced forms (observable.ref, action.bound, flow.bound, computed.struct, comparer.*) are gone in favour of named exports and are now type errors; makeObservable's annotations argument is required; legacy decorators are removed; computed(fn) returns an IComputedValue without set() (7.1.0). AnnotationsMap and AdditionalKeys are unchanged.

    What the page store uses (53 classes, 47 annotation maps, 317 entries)

    • computed on getters 164; action 91 (66 arrow fields, 22 async arrow fields, 3 prototype methods); observable 58; observableRef 1 (Run.js:64); override 3 (AlignmentOptions.js:120, AlignmentOptions.js:137, RunOptions.js:441).
    • No flow, flowResult, actionBound, observableShallow, makeAutoObservable, options argument, AdditionalKeys generic, private or #private member. So the non-public and flow typing cases do not arise in the current store.

    One finding outside the types. Jest's Babel transform uses @babel/preset-typescript 7 without allowDeclareFields, which deletes type-only class fields (confirmed by transforming a sample). Option.value (Option.js:43) is declared without an initializer and annotated before it is assigned, so as a typed field with no initializer in a .ts file it would hit MobX's "Field not found" runtime error under Jest.

    Not verified: what the Vite renderer build emits for that same type-only field; mobx-utils 6.1.1 typings against MobX 7; runtime error strings are quoted from source, not triggered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

wayfinder:research

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions