打字稿中数组的两种写法有什么区别

What's the Difference between the two of writings of arrays in typescript

TypeScript, like JavaScript, allows you to work with arrays of values. Array types can be written in one of two ways. In the first, you use the type of the elements followed by '[]' to denote an array of that element type:

var list:number[] = [1, 2, 3];

The second way uses a generic array type, Array:

var list:Array<number> = [1, 2, 3];

这两种语法有区别吗?有 2 种编写方法的充分理由吗?(我知道泛型在其他语言中是一回事,但我从未在过去的任何语言中使用过泛型)。

Documentation

它们是等价的。对于 Array<number> 泛型,number[] 形式是 shorthand。

section 3.7.4 of the spec 中的 "array type literal" 定义为:

An array type literal references an array type (section 3.3.2) with the given element type.

来自section 3.3.2的"array type"描述为:

Array types are named type references created from the generic interface type 'Array' in the global module with the array element type as a type argument.

这说明数组被定义为带有参数的泛型类型。通用是最终版本,但该描述后紧跟:

Array type literals (section 3.7.4) provide a shorthand notation for creating such references.

没有区别,type[] 表单作为 shorthand 使用预先存在的 JS 模式存在。