In TypeScript can declare strongly-typed Array Types
But errors (from compiller) for the next snippet very strange. Could someone explain me error cases?
interface SomeArray1 {
[index: number]: string;
}
var my1: SomeArray1;
my1["ab"] = 12; // <-- why this line is OK?
my1["ab"] = "ab"; // <-- why this line is OK?
my1[12] = "ab";
my1[12] = 12; // <-- error
//-----------------------------
interface SomeArray2 {
[index: string]: number;
}
var my2: SomeArray2;
my2["ab"] = 12;
my2["ab"] = "ab"; // <-- error
my2[12] = "ab"; // <-- error
my2[12] = 12;
//-----------------------------
interface SomeArray3 {
[index: string]: string;
}
var my3: SomeArray3;
my3["ab"] = 12; // <-- error
my3["ab"] = "ab";
my3[12] = "ab";
my3[12] = 12; // <-- error
Open sample in playground