title: Typescript - "... is not a function or its return value is not iterable"
body: <p>I have this helper function:</p>
  
  <pre><code>export function to(promise: Promise&lt;any&gt;) {
    return promise
      .then((data: any) =&gt; [null, data])
      .catch((err: Error) =&gt; [err, null]);
  }
  </code></pre>
  
  <p>This function (in theory) should help me catch errors while using <code>await</code> in functions. for example:</p>
  
  <pre><code>const [err, data] = await to(validate(card));
  </code></pre>
  
  <p>The problem is that on runtime, I get the following error:</p>
  
  <blockquote>
    <p>to is not a function or its return value is not iterable</p>
  </blockquote>
  
  <p>While the expected return signature should be <code>Promise&lt;[Error, null]&gt; Promise&lt;[null, Error]&gt;</code>, it looks like returns (again, in theory, because it actually fails): <code>Promise&lt;any[] | Error[]&gt;</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/uIsMg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uIsMg.png" alt="function to"></a></p>
  
  <p>What am I missing?</p>