title: How can I narrow down a return type to a more specific type in typescript?
body: <p>This came up when working with the mysql2/promise library in TypeScript.</p>
  <p>Consider the following (abbreviated) Snippet:</p>
  <pre class="lang-js prettyprint-override"><code>import {FieldPacket, PoolConnection, RowDataPacket} from 'mysql2/promise';
  
  let connection: PoolConnection = await promisePool.getConnection();
  
  connection.query(FETCH_USER_ID)//returns Promise&lt;[(RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[] | ResultSetHeader), FieldPacket[]]&gt;
      .then((results: [RowDataPacket,FieldPacket[]]) =&gt; {
          fields.UserID = results[0].value;
      });
  </code></pre>
  <p>From the semantics of my query, I know for a fact that in this specific case the return type will indeed be <code>[RowDataPacket,FieldPacket[]]</code>, but TypeScript does not allow that assignment because it is of course formally wrong.</p>
  <p>The case where formally a number of types <em>could</em> apply, but semantically only one of them or a subset thereof are possible in any given situation can't be that rare, so I assume there must be a solution.</p>
  <p>How can I tell TypeScript to expect a subset of the theoretically possible types, preferably without resorting to any or ts-ignore?</p>