Typescript Array vs any[]

后端 未结 2 717
半阙折子戏
半阙折子戏 2020-12-14 07:18

In TypeScript, what is the difference between Array and any[]? Does Array refer to dynamically sized arrays (during compile-time, of course) and

2条回答
  •  感情败类
    2020-12-14 07:36

    Spec section 3.5.4 specifies their relationship:

    An array type of the form ElementType[] is equivalent to an object type with the index signature [index: number]: ElementType plus a set of members equivalent to the global interface type Array where all occurrences of the magic _element type are replaced with ElementType.

    Not being able to assign [[], []] or [] to Array is a bug.

    There is no notion of a "statically-sized" array in TypeScript. They're all talking about the same underlying JavaScript array (which is dynamically sized):

    var x = [1, 2];
    x.push(3);
    x[13] = 5;
    

提交回复
热议问题