3
votes

Why is this illegal in TypeScript?

interface numarr {
    [i : number] : number;
}

var p : numarr = [3,6,8];

The compiler says "Cannot convert 'number[]' to 'numarr'."

I'm afraid I've misunderstood something quite basic here. I thought the point of the above interface was to describe an array of numbers indexed by numbers, which is exactly what [3,6,8] is.

2

2 Answers

3
votes

You can alternatively use the following syntax, if I didn't get you wrong.

var arr : number[] = [3, 6, 8];
1
votes

I asked this question in 2013. It is no longer relevant. The code in my question is now accepted by the compiler.