Type inference based on index signatures

Topics: General, Language Specification
Aug 28, 2013 at 9:43 PM
I want to define a function that iterates over array like objects i.e. object that have a number index and a length property.
interface L<T> {
    [index: number]: T;
    length: number;
}

declare function f<T>(l: L<T>): T[];
My problem is that I can't get it to work properly with JQuery objects (J's definition below is based on how the JQuery interface is defined in the JQuery type file):
interface J {
    length: number;
    [x: string]: any;
    [x: number]: HTMLElement;
}

var a = [1,2,3];
var j: J;

var j1 = j[0];
var j2 = j['a'];

var t1 = f(a);
var t2 = f(j);
In the resulting declaration file j1 is inferred as HTMLElement as expected but t2 is inferred as any[] while I expected it to be HTMLElement[]:
interface L<T> {
    [index: number]: T;
    length: number;
}
interface J {
    length: number;
    [x: string]: any;
    [x: number]: HTMLElement;
}
declare var a: number[];
declare var j: J;
declare var j1: HTMLElement;
declare var j2;
declare var t1: number[];
declare var t2: any[];

if I remove the line [x: string]: any; from the definition of J then t2 is inferred as HTMLElement[] as expected. Why does [x: string]: any prevent correct inference of t2?