title: TypeScript interface to cover Array and Typed Arrays
body: <p>I'd like to use TypeScript to declare functions with parameters that can be either a regular JavaScript Array, or a Typed Array, but I can't find a nice way to do it.  I was hoping that an interface like:</p>
  
  <pre><code>interface IArray
  {
      length: number;
      [index: number]: number;
  };
  </code></pre>
  
  <p>would let me declare a function:</p>
  
  <pre><code>declare var vAdd: { (a: IArray, b: IArray): IArray; };
  </code></pre>
  
  <p>and use it in any of the following ways:</p>
  
  <pre><code>var y;
  y = vAdd(new Float32Array([1,2,3]), new Float32Array([1,2,3]));
  y = vAdd([1,2,3], new Float32Array([1,2,3]));
  y = vAdd(new Float32Array([1,2,3]), new Array(1,2,3));
  </code></pre>
  
  <p>... etc.  However, only the first line here works (in the TypeScript playground, at least).  The others generate an error saying the arguments don't match the function signature.</p>
  
  <p>By overloading the function it can be made to work:</p>
  
  <pre><code>declare var vAdd: { (a: IArray, b: IArray): IArray;
                      (a: number[], b: IArray): IArray;
                      (a: IArray, b: number[]): IArray; 
                      (a: number[], b: number[]): IArray; };
  </code></pre>
  
  <p>but I'm wondering if there is a way I can avoid writing out all combinations of IArray and number[], which will get even more tedious for functions with more parameters.  Or is there something fundamentally wrong with what I'm trying to do?</p>