问题
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