npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

mobx-activator

v2.0.0

Published

Typescript compiler transformer to enhance your types automatically when using the new Mobx 6.

Readme

mobx-activator

Typescript compiler transformer to enhance your types automatically when using the new Mobx 6.

Dependencies

  • Mobx 6+

What does this tool?

If you are following and plan to use the new Mobx 6, you will realize that you have two new ways of defining your model as observable.

  1. Using the superb makeAutoObservable;
  2. Using the good makeObservable.

So, why do I have to use the mobx-activator? Well, we have some small problems here.

  • First, you can't use the makeAutoObservable if your models have inheritance, but you still can use the makeObservable;
  • When using the makeObservable, you have to take some caution to only annotate as observable the properties of your current type;
    • And, the thing could be very annoying if you have a big model with a lot of properties and actions (Yeah! You should have broken that beast but now it's too late!!);
    • And, to finish, you need to initialize all your properties, even those that will have the value undefined by default.

While the mobx team is doing an excellent job on it. There is some things that you can only do automatically when analyzing the structure of your type (the AST). That is exactly what this library does! It analyses your classes, remove that @reactive decorator, and call the makeObservable inside of the Type's constructor, annotating all properties, accessors and methods automatically for you.

Ok, I'm lying about a small detail. The makeObservable is not being called directly, but through the reactive.enhance function. This function will analyzing your type at runtime and initialize all the annotated properties with undefined if you haven't done that yet.

Show me the code

If you have something like this:

import { reactive } from 'mobx-acttivator'

@reactive
class Pilot {
  constructor(
    public name: string,
    public surname: string,
    public age: number) {

  }

  get isAdult() {
    return age >= 18 // ok, this is valid at least for Brazil!
  }

  setName(value: string) {
    this.name = value
  }

  setSurname(value: string) {
    this.surname = value
  }

  setAge(value: number) {
    this.age = value
  }
}

This will be transformed to:

import { reactive } from 'mobx-acttivator'

class Pilot {
  constructor(
    public name: string,
    public surname: string,
    public age: number) {

    reactive.enhance(Pilot, this, [
      [['name', false], ['surname', false], ['age': false]],
      ['isAdult'],
      ['setName', 'setSurname', 'setAge']
    ])
  }

  get isAdult() {
    return age >= 18 // ok, this is valid at least for Brazil!
  }

  setName(value: string) {
    this.name = value
  }

  setSurname(value: string) {
    this.surname = value
  }

  setAge(value: number) {
    this.age = value
  }
}

which would be the equivalent of writing by yourself the following:

import { makeObservable, observable, computed, action } from 'mobx'

class Pilot {
  constructor(
    public name: string,
    public surname: string,
    public age: number) {

    makeObservable(this, {
      name: observable,
      surname: observable,
      age: observable,
      isAdult: computed,
      setName: action,
      setSurname: action,
      setAge: action
    })
  }

  get isAdult() {
    return age >= 18 // ok, this is valid at least for Brazil!
  }

  setName(value: string) {
    this.name = value
  }

  setSurname(value: string) {
    this.surname = value
  }

  setAge(value: number) {
    this.age = value
  }
}

So, in the end, this can save your fingers a bit.

Usage

  1. First, you need to add the transformer to your typescript compiler pipeline;

If you are using ts-loader with webpack, then you should add the transformer to the getCustomTransformers option of the bundler:

{
  test: /\.tsx?$/,
  loader: 'ts-loader',
  options: {
    getCustomTransformers: program => ({
      before: [
        require('mobx-activator/lib/transformers/ts-transformer').transform()
      ]
    })
  }
}

If you are using jest with ts-jest, then you should add the transformer to the ts-jest's globals configuration:

module.exports = {
  preset: 'ts-jest',
  globals: {
    'ts-jest': {
      astTransformers: [
        'mobx-activator/lib/transformers/ts-jest-transformer'
      ]
    }
  }
}
  1. Just decorate your classes with the reactive decorator:
import { reactive } from 'mobx-activator';

@reactive
class MyThing {
}

Don't forget to allow the experimentalDecorators in your tsconfig.json.

Overriding the current configuration

The reactive decorator accepts the same parameter of the second parameter of the mobx's makeObservable, except that you can specify null for a specific property to make it not observable, by not applying the annotation that property.


@reactive({
  id: null
})
class MyThing {
  constructor(
    readonly id: string,
    public name: string) {
  }
}

Default behavior

The reactive has some default configurations, like:

import { reactive } from 'mobx-activator'

/**
 * When true, all your actions will be annotated with the `action.bind` by default.
 */
reactive.options.autoBind = false /* default value */

/**
 * When true, if you define a property as readonly, in TypeScript,
 * then we will use the `observable.ref` instance of just the `observable`.
 */
reactive.options.readOnlyAsObservableRef = true; /* default value */