title: Super type constraints in TypeScript
body: <p>I'm trying to use the <code>updeep</code> library.  A typical example of using <code>updeep</code> is something like this:</p>
  
  <pre><code>var person = {
    name: {
      first: 'Jane',
      last: 'West'
    }
  };
  
  var result = u({ name: { first: 'Susan' } }, person);
  </code></pre>
  
  <p>The idea here is that <code>result</code> will be a clone of <code>person</code> but with the value of <code>name.first</code> changed.  You can imagine this function, <code>u</code>, defined in TypeScript as:</p>
  
  <pre><code>function u&lt;T&gt;(changes: {}, obj: T): T { ... }
  </code></pre>
  
  <p>This captures the fact that the type of the second argument is also the return type of the function.  But what it does not express is that <code>changes</code> should be a <strong>super type</strong> of <code>T</code>.</p>
  
  <p>What I'd like is some type checking on the first argument.  The point is that the values present in the <code>changes</code> arguement should all be present in the <code>T</code> type parameter and match the types of their counterparts there.  Expressing this allows us to check the <code>changes</code> argument to make sure it makes sense with respect to the type <code>T</code> (<em>i.e.,</em> is a super type of <code>T</code>).</p>
  
  <p>I'm not sure if this is possible in TypeScript.  In languages like Java, you have the <code>super</code> keyword and it can be used to describe constraints on type parameters.</p>
  
  <p>While TypeScript doesn't allow it, something like this expresses what I would like:</p>
  
  <pre><code>function u&lt;T extends U,U extends {}&gt;(changes: U, obj: T): T { ... }
  </code></pre>
  
  <p>Does anybody have an suggestions on how to express this?  It would be great to have a typesafe system for performing such transformations.</p>
  
  <p>Thanks.</p>