title: How do I declare a Generic Promise in Typescript, so that when the Generic type is "<void>", one of its methods won't take a parameter?
body: <p>In Typescript, I want to be able to define a Promise's type in such a way so that I can do this:</p>
  
  <pre><code>//This works today:
  new Promise&lt;number&gt;((resolve)=&gt;{
     //Cool
     resolve(5);
     //Error, because I didn't pass a number:
     resolve();
  }
  
  //This is what I want to do also:
  new Promise&lt;void&gt;((resolve)=&gt;{
     //Error, because I passed a value:
     resolve(5);
     //Cool, because I declared the promise to be of type void, so resolve doesn't take a value:
     resolve();
  }
  </code></pre>
  
  <p>The promise definition files I've seen all declare that the "resolve" method of a promise must take a value. <a href="https://github.com/borisyankov/DefinitelyTyped/blob/master/es6-promise/es6-promise.d.ts">Here</a> is a recent example from the wonderful DefinitelyTyped project:</p>
  
  <pre><code>declare class Promise&lt;R&gt; implements Thenable&lt;R&gt; {
      constructor(callback: (resolve : (result: R) =&gt; void, reject: (error: any) =&gt; void) =&gt; void); 
  ///...
  }
  </code></pre>
  
  <p>```</p>
  
  <p>That basically says, "The resolve callback must be passed a value of type R." That's fine for a promise like <code>new Promise&lt;number&gt;</code>. Typescript will verify we're calling resolve with a value of type <code>number</code>.</p>
  
  <p>However, what if I want a promise that doesn't have a value, so I want to be able to call resolve() without passing a value? I can declare my promise like this: <code>new Promise&lt;void&gt;</code>
  But then I'm still forced to call resolve and pass in a value of some sort. I can call <code>resolve(undefined)</code>, but that reads a bit strangely.</p>
  
  <p>There appears to be no way to properly capture this concept in Typescript: "If this generic has a type 'void', then don't expect a parameter for this function."</p>
  
  <p>The closest I can do is mark the result as optional in the resolve method, but that would mean that the result is <em>always</em> optional, even for typed versions of Promises.</p>