Differences in Typescript array literal syntax

本小妞迷上赌 提交于 2019-12-12 12:07:18

问题


Typescript allows one to define an Array with either syntax:

var myStrArry1: string[] = [];

or

var myStrArry1: Array<string> = [];

The compiled output appears to be the same. Does the compiler treat them identically, or are there some quirks to be aware of?


回答1:


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

They are identical. I prefer syntax 1




回答2:


From the typescript documentation, they are treated identically, one is simply a shorthand notation for the other. The compiler doesn't care which one you use.




回答3:


This is the paragraph from TypeScript specification: https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#3.8.4 that specifies that both syntaxes are equivalent:

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

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



来源:https://stackoverflow.com/questions/34887022/differences-in-typescript-array-literal-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!