title: Regressive generic type inference
body: <p><a href="http://www.typescriptlang.org/play/#src=interface%20IService%20%7B%0D%0A%7D%0D%0A%0D%0Afunction%20isOverriden%3CT%20extends%20BaseService%2C%20U%20extends%20IService%3E(target%3A%20T%2C%20prop%3A%20keyof%20T%20%7C%20keyof%20U)%3A%20prop%20is%20keyof%20T%20%7B%0D%0A%09return%20prop%20in%20target%3B%0D%0A%7D%0D%0A%0D%0Aabstract%20class%20BaseService%20implements%20IService%20%7B%0D%0A%09constructor(private%20readonly%20origin%3A%20IService)%20%7B%20%7D%0D%0A%09static%20wrap%3CU%20extends%20IService%2C%20T%20extends%20BaseService%3E(this%3A%20%7B%20new(origin%3A%20U)%3A%20T%20%7D%2C%20origin%3A%20U)%3A%20T%20%26%20U%20%7B%0D%0A%09%09return%20new%20Proxy(new%20this(origin)%20as%20T%20%26%20U%2C%20%7B%0D%0A%09%09%09get(target%3A%20T%20%26%20U%2C%20prop%3A%20keyof%20T%20%7C%20keyof%20U)%20%7B%0D%0A%09%09%09%09if%20(isOverriden(target%2C%20prop))%20return%20target%5Bprop%5D%3B%0D%0A%09%09%09%09return%20target.origin%5Bprop%5D%3B%0D%0A%09%09%09%7D%0D%0A%09%09%7D)%0D%0A%09%7D%0D%0A%7D%0D%0A%0D%0Aclass%20SimpleService%20implements%20IService%20%7B%0D%0A%0D%0A%7D%0D%0A%0D%0Aclass%20MongoService%20implements%20IService%20%7B%0D%0A%09log()%20%7B%0D%0A%09%09console.log(&#39;Hello%201&#39;)%3B%0D%0A%09%7D%0D%0A%7D%0D%0A%0D%0Aclass%20VerificationService%20extends%20BaseService%20%7B%0D%0A%09log2()%20%7B%0D%0A%09%09console.log(&#39;Hello%202&#39;)%3B%0D%0A%09%7D%0D%0A%7D%0D%0A%0D%0Aconst%20serviceInferedIncorrectly%20%3D%20VerificationService.wrap(new%20MongoService())%3B%20%2F%2F%20VerificationService%20%26%20IService%0D%0Aconst%20serviceInferedCorrectly%20%3D%20VerificationService.wrap(new%20SimpleService())%3B%20%2F%2F%20VerificationService%20%26%20SimpleService%0D%0Aconst%20serviceSpecified%20%3D%20VerificationService.wrap%3CMongoService%2C%20VerificationService%3E(new%20MongoService())%3B%20%2F%2F%20VerificationService%20%26%20MongoService%0D%0A%0D%0A" rel="nofollow noreferrer">Demo code in the TypeScript playground</a></p>
  
  <p>I'm trying to create a more dynamic implementation of the decorator pattern using the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy" rel="nofollow noreferrer">ES6's <code>Proxy</code> object</a>.</p>
  
  <p>The general idea is that I have an <code>IService</code> interface and a <code>BaseService</code> abstract class. The abstract class has an <code>origin</code> property that's an instance of <code>IService</code> and all the unimplemented calls are forwarded to this object. <code>BaseService</code> exposes a static method <code>wrap</code> that creates a new instance of the decorator and sets it's origin to the service instance provided as a parameter.</p>
  
  <p>Functionality of this approach is not a problem, the problem comes when I try to specify the types. For some reason, when I try to decorate the <code>MongoService</code> with a <code>VerificationService</code>, the origin type is reduced to <code>IService</code> and the return value is of type <code>VerificationService &amp; IService</code> instead of the desired <code>VerificationService &amp; MongoService</code>.</p>
  
  <p>My question is whether this is intended behavior because of some covariance/contra-variance issues the code could lead to or if it's a bug and the compiler just doesn't know that the type of the parameter is MongoService. Please notice that when I try to decorate an instance of a class that doesn't add any properties, the type is correctly inferred (line 37)</p>