title: Implicit or Explicit return for a Mutation Identity Function
body: <p>I have the desire to allow users compose mutation functions in advance of the state they are mutating actually being available and I've created a higher order function called <code>MutationIdentity</code>:</p>
  <pre><code>type MutationIdentity&lt;T, P extends any[]&gt; = (state: T) =&gt; (...args: P) =&gt; T;
  </code></pre>
  <p>This expresses a type which represents both the state <code>T</code> which is being mutated and the parameters <code>P</code> in the interior function. While I find it a useful expression of the intended shape of the identity mutation (sorry if this is imprecise functional language ... i'm relative new to the terminology), it can not be inferred directly due to limitations of Typescript.</p>
  <p>So for construction of these identity mutations I have come up with the following function:</p>
  <pre><code>function MutationIdentity&lt;T&gt;() {
    return function &lt;M extends MutationIdentity&lt;T, P&gt;, P extends any[]&gt;(mutationFn: M) {
      return mutationFn;
    };
  }
  </code></pre>
  <p>This allows <em>type-strong</em> construction of these functions and returns an <em>implicit</em> representation of <code>MutationIdentity</code> type referenced above:</p>
  <pre><code>type State = { foo: number, bar: number };
  const incr = MutationIdentity&lt;State&gt;()(
      s =&gt; (foo: number) =&gt; ({ ...s, foo: s.foo++ })
  );
  </code></pre>
  <p>I have two questions but the primary question is this ... would this function be better if it returned an explicit representation of <code>MutationIdentity&lt;T,P&gt;</code> or is <em>implicit</em> preferred? Is this possible?</p>
  <p>Just to clear, the <code>incr</code> function defined above can be explicitly typed like so:</p>
  <pre><code>const incr: MutationIdentity&lt;State, [number]&gt; = MutationIdentity&lt;State&gt;()(
      s =&gt; (foo: number) =&gt; ({ ...s, foo: s.foo++ })
  );
  </code></pre>
  <p>and this causes no typing issues but it's just that the type is not being cast to this explicit form currently.</p>
  <p>The secondary question is more stylistic but ... do people think the definition of a <em>type</em> and <em>function</em> which share the name <strong>MutationIdentity</strong> is a code smell or is this ok? Naming is hard and the function returns the type by the same name. Anyway, interested in views on this.</p>
  <p>Full code including typing tests can be found in this <a href="https://tsplay.dev/mplBxm" rel="nofollow noreferrer">playground</a></p>