Are object index signatures equivalent to array types?

Section 3.5.4 of the specification states: "The array type of the ElementType [] form is equivalent to the type of the object with an index signature [index: number]: ElementType", but this does not seem to be the case, for example

var a: {[index: number]: string;}; var b: string[]; a = ['1','2']; // ERROR: Cannot convert 'string[]' to '{ [index: number]: string; }' b = ['1','2']; // OK 

What am I missing here?

+8
typescript
source share
1 answer

Yes, this is a known bug in the compiler. This will be fixed in a future version. The best workaround is to cast (on both sides of the destination - a = <string[]>['1', '2'] , probably looks a little less strange).

+1
source share

All Articles