title: Typed generic action in MobX-State-Tree
body: <p>You can modify a MobX-state-tree node with the help of <a href="https://mobx-state-tree.js.org/concepts/actions" rel="nofollow noreferrer">actions</a>. This is generally great, but it can become cumbersome if you have a lot of different properties that you want to set, e.g. if you have a model representing a form.</p>
  <p>Is there a way to write a generic action that takes a property name and a value as arguments, and sets the property to the value on the node, while still having TypeScript verify that the arguments are valid?</p>
  <p><strong>Example</strong></p>
  <pre class="lang-js prettyprint-override"><code>import { types } from &quot;mobx-state-tree&quot;;
  
  const FormModel = types
    .model({
      email: &quot;&quot;,
      password: &quot;&quot;,
      username: &quot;&quot;
    })
    .actions((self) =&gt; ({
      // This becomes cumbersome as the model grows
      setEmail(email: string) {
        self.email = email;
      },
      setPassword(password: string) {
        self.password = password;
      },
      setUsername(username: string) {
        self.username = username;
      }
    }));
  </code></pre>