title: Typescript generic restricted by extend, returned from function
body: <p>I'm not sure how to put this into words, so here is an example</p>
  
  <pre><code>type NonVoid = Object | object;
  
  type Something&lt;T&gt; = {
      test: string
  } &amp; (T extends NonVoid ? {
      requiredWhenNonVoid: T
  } : {
      requiredWhenNonVoid?: undefined
  });
  
  
  function testFunc1&lt;A extends NonVoid&gt;(a: A): Something&lt;NonVoid&gt; {
      return {
          test: 'test',
          requiredWhenNonVoid: a
      };
  }
  
  function testFunc2&lt;A extends NonVoid&gt;(a: A): Something&lt;A&gt; {
      return {
          test: 'test',
          requiredWhenNonVoid: a // &lt;-- why error here? A cannot be void??
      };
  }
  </code></pre>
  
  <p>On playground: <a href="http://www.typescriptlang.org/play/#src=type%20NonVoid%20%3D%20Object%20%7C%20object%3B%0D%0A%0D%0Atype%20Something%3CT%3E%20%3D%20%7B%0D%0A%20%20%20%20test%3A%20string%0D%0A%7D%20%26%20(T%20extends%20NonVoid%20%3F%20%7B%0D%0A%20%20%20%20requiredWhenNonVoid%3A%20T%0D%0A%7D%20%3A%20%7B%0D%0A%20%20%20%20requiredWhenNonVoid%3F%3A%20undefined%0D%0A%7D)%3B%0D%0A%0D%0A%0D%0Afunction%20testFunc1%3CA%20extends%20NonVoid%3E(a%3A%20A)%3A%20Something%3CNonVoid%3E%20%7B%0D%0A%20%20%20%20return%20%7B%0D%0A%20%20%20%20%20%20%20%20test%3A%20&#39;test&#39;%2C%0D%0A%20%20%20%20%20%20%20%20requiredWhenNonVoid%3A%20a%0D%0A%20%20%20%20%7D%3B%0D%0A%7D%0D%0A%0D%0Afunction%20testFunc2%3CA%20extends%20NonVoid%3E(a%3A%20A)%3A%20Something%3CA%3E%20%7B%0D%0A%20%20%20%20return%20%7B%0D%0A%20%20%20%20%20%20%20%20test%3A%20&#39;test&#39;%2C%0D%0A%20%20%20%20%20%20%20%20requiredWhenNonVoid%3A%20a%20%2F%2F%20%3C--%20why%20error%20here%3F%20A%20cannot%20be%20void%3F%3F%0D%0A%20%20%20%20%7D%3B%0D%0A%7D" rel="nofollow noreferrer">click here</a></p>
  
  <p>The question is, <strong>why does <code>testFunc2</code> have the following error</strong>?</p>
  
  <blockquote>
    <p>Type '{ test: string; requiredWhenNonVoid: A; }' is not assignable to type 'Something'.
      Object literal may only specify known properties, but 'requiredWhenNonVoid' does not exist in type 'Something'. Did you mean to write 'requiredWhenNonVoid'?</p>
  </blockquote>
  
  <p>It seems that the compiler doesn't know that <code>Something</code> can only be</p>
  
  <pre><code>type Something&lt;T&gt; = {
      test: string
  } &amp; {
      requiredWhenNonVoid: T
  };
  </code></pre>
  
  <p>in that case.</p>
  
  <p>Is this something that should be classed as a bug and can be fixed in the compiler, or am I missing something?</p>