title: Awaitable type in TypeScript
body: <p>I use async / await a lot in JavaScript. Now I’m gradually converting some parts of my code bases to TypeScript.</p>
  
  <p>In some cases my functions accept a function that will be called and awaited. This means it may either return a promise, just a synchronous value. I have defined the <code>Awaitable</code> type for this.</p>
  
  <pre class="lang-js prettyprint-override"><code>type Awaitable&lt;T&gt; = T | Promise&lt;T&gt;;
  
  async function increment(getNumber: () =&gt; Awaitable&lt;number&gt;): Promise&lt;number&gt; {
    const num = await getNumber();
    return num + 1;
  }
  </code></pre>
  
  <p>It can be called like this:</p>
  
  <pre><code>// logs 43
  increment(() =&gt; 42).then(result =&gt; {console.log(result)})
  
  // logs 43
  increment(() =&gt; Promise.resolve(42)).then(result =&gt; {console.log(result)})
  </code></pre>
  
  <p>This works. However, it is annoying having to specify <code>Awaitable</code> for all of my projects that use async/await and TypeScript.</p>
  
  <p>I can’t really believe such a type isn’t built in, but I couldn’t find one. Does TypeScript have a builtin awaitable type?</p>