title: Typescript check if object is iterable
body: <p>I have a class where I want to have a method to add one or more objects to a private collection. The way I want to do this is with overloads and implementation so that callers can use the same method for adding one object as for a collection of objects. I currently have it working like this:</p>
  
  <pre><code>import { InterfaceType } from "some/data-model/directory";
  
  export class MyClass {
      private _collection: Array&lt;InterfaceType&gt;;
  
      constructor() {
          this._collection = [];
      }
  
      /**
       * Adds a new InterfaceType to the stored InterfaceTypes.
       * @param it The new InterfaceType to be stored.
       */
      public push(it: InterfaceType): void;
  
      /**
       * Adds new InterfaceTypes to the stored InterfaceTypes.
       * @param its The collection of new InterfaceTypes to be stored.
       */
      public push(its: Iterable&lt;InterfaceType&gt;): void;
  
      public push(i: InterfaceType | Iterable&lt;InterfaceType&gt;): void {
          if (typeof(i[Symbol.iterator]) === "function") {
              this._collection = this._collection.concat(Array.from(i as Iterable&lt;InterfaceType&gt;));
              return;
          }
          this._collection.push(i as InterfaceType);
      }
  </code></pre>
  
  <p>But note the <code>as</code> statements. I didn't need to add those when I had it typed as a <code>InterfaceType | Array&lt;InterfaceType&gt;</code> because Typescript recognized when I checked <code>i instanceof Array</code>, but because <code>Iterable</code> is an interface I can't directly check <code>instanceof</code>.</p>
  
  <p>So my question is: how do I get Typescript to recognize that I have checked for and handled the case that a parameter is an <code>Iterable</code> without resorting to <code>as</code>? Is there no way?</p>