title: Typescript interface generics extends
body: <p>Given the following interface:</p>
  
  <pre><code>interface IMyInterface&lt;T extends { [key: string]: number }&gt; {
      target: T;
      to:  T;
  }
  </code></pre>
  
  <p>I want to change it to something like</p>
  
  <pre><code>interface IMyInterface&lt;T extends { [key: string]: number }&gt; {
      target: P extends T;
      to:  T;
  }
  </code></pre>
  
  <p>where target can be any object as long as it contains the properties defined in T.</p>
  
  <p>example:</p>
  
  <pre><code>let obj = &lt;IMyInterface&gt;{
      target: { name:"John", age: 100 },
      to: { age: 200 }
  }
  </code></pre>
  
  <p>The typescript compiler should complain in a situation like:</p>
  
  <pre><code>let obj = &lt;IMyInterface&gt;{
      target: { name:"John", y: 1980 },
      to: { x: 20 }
  }
  </code></pre>
  
  <p>where the "x" property is missing in "target"</p>
  
  <p>and </p>
  
  <pre><code>let obj = &lt;IMyInterface&gt;{
      target: { name:"John", x: "20" },
      to: { x: 30 }
  }
  </code></pre>
  
  <p>where the type is wrong(number vs string)</p>
  
  <p>i.e. I want typechecking for the target, so that it contains all the properties defined in the "T" generics.</p>
  
  <p>And I want this as an object.</p>
  
  <p>Creating a method like</p>
  
  <pre><code>methodName&lt;T extends { [key: string]: number }&gt;(target: T, to: T){
  
  }
  // and calling it
  methodName({ name:"John", x: "20" },{x:30});
  </code></pre>
  
  <p>i.e. with separate arguments for "target" and "to" works and the typechecking in Typescript is perfect, but I need "target" and "to" enclosed in an object so that I can have a method like:</p>
  
  <pre><code>methodName&lt;IMyInterface&gt;(object: IMyInterface){
  </code></pre>
  
  <p>and calling it like this:</p>
  
  <pre><code>methodName({ target: { name:"John", age:20 }, to: { age: 30}});
  </code></pre>
  
  <p>Thanks for your help</p>