title: Creating a generic method decorator in Typescript
body: <p>In this code, I am trying to have a user specify a type for generic decorator function fetchJson. </p>
  
  <p>It would work like this:</p>
  
  <ol>
  <li>Decorate a method with something like: <code>@fetchJson&lt;User&gt;</code></li>
  <li>We then replace the function with one that automatically calls <code>.then(res =&gt; res.json())</code>, and give back a typed value wrapped in a Promise.</li>
  </ol>
  
  <p>The issue I am running into is that I do not know how to assign the return <code>descriptor.value</code> to a user-assigned T.
  Is there a better way to do this? I feel like I am missing something entirely.</p>
  
  <pre><code>interface PromiseDescriptorValue&lt;T&gt;{
    (...args: any[]): Promise&lt;T&gt;;
  }
  
  const fetchJson = &lt;T&gt;(
    target: Object,
    propertyKey: string,
    descriptor: TypedPropertyDescriptor&lt;PromiseDescriptorValue&lt;Response&gt;&gt; // Response is a whatwg-fetch response -- https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/whatwg-fetch/whatwg-fetch.d.ts#L58
  ): TypedPropertyDescriptor&lt;PromiseDescriptorValue&lt;T&gt;&gt; =&gt; {
    const oldMethod = descriptor.value;
  
    descriptor.value = function(...args: any[]) {
      return oldMethod.apply(this, args).then((res: Response) =&gt; res.json());
    };
  
    return descriptor;
  };
  
  
  // TS2322: Type 'TypedPropertyDescriptor&lt;PromiseDescriptorValue&lt;Response&gt;&gt;'
  // is not assignable to type
  // 'TypedPropertyDescriptor&lt;PromiseDescriptorValue&lt;T&gt;&gt;'. Type
  // 'PromiseDescriptorValue&lt;Response&gt;' is not assignable to type
  // 'PromiseDescriptorValue&lt;T&gt;'. Type 'Response' is not assignable to type 'T'.
  </code></pre>