简体   繁体   English

打字稿中的两个数组作品有什么区别

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

TypeScript, like JavaScript, allows you to work with arrays of values. 像JavaScript一样,TypeScript允许您使用值数组。 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: 第二种方法使用通用数组类型Array:

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

Are there any difference between these two syntax? 这两种语法之间有什么区别吗? Is there a good reason for 2 ways to write these? 有两种写这些方法的充分理由吗? (I know generics are a thing in other languages but I've never used generics in any past lang). (我知道泛型在其他语言中是一回事,但我从未在过去的lang中使用过泛型)。

Documentation 文档

They are equivalent. 它们是等效的。 The number[] form is shorthand for the Array<number> generic. number[]形式是Array<number>泛型的简写形式。

The "array type literal" from section 3.7.4 of the spec is defined as: 规范第3.7.4节中的“数组类型文字”定义为:

An array type literal references an array type (section 3.3.2) with the given element type. 数组类型文字会引用具有给定元素类型的数组类型(第3.3.2节)。

The "array type" from section 3.3.2 are described as: 第3.3.2节中的“数组类型”描述为:

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”创建的命名类型引用,其中数组元素类型为类型实参。

This notes that arrays are defined as a generic type with an argument. 这说明将数组定义为带有参数的泛型类型。 The generic is the definitive version, but that description is followed immediately by: 泛型是确定的版本,但是紧随其后的是:

Array type literals (section 3.7.4) provide a shorthand notation for creating such references. 数组类型文字(第3.7.4节)提供了创建此类引用的简写符号。

There is no difference, the type[] form exists as a shorthand using pre-existing JS patterns. 没什么区别, type[]形式是使用预先存在的JS模式的简写形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM