Typescript 数组文字语法的差异

Differences in Typescript array literal syntax

Typescript 允许使用任一语法定义数组:

var myStrArry1: string[] = [];

var myStrArry1: Array<string> = [];

编译后的输出看起来是一样的。编译器是否以相同的方式对待它们,或者是否有一些需要注意的怪癖?

Does the compiler treat them identically, or are there some quirks to be aware of?

它们是相同的。我更喜欢语法 1

typescript documentation开始,它们被同等对待,一个只是另一个的shorthand符号。编译器不关心你使用哪一个。

这是来自 TypeScript 规范的段落:https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.8.4 指定两种语法是等效的:

Alternatively, array types can be written using the Array<T> notation. For example, the types above are equivalent to

Array<string | number> Array<() => string>