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

Armeen Harwood

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];

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).

Documentation

ssube

They are equivalent. The number[] form is shorthand for the Array<number> generic.

The "array type literal" from section 3.7.4 of the spec is defined as:

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

The "array type" from section 3.3.2 are described as:

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.

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.

There is no difference, the type[] form exists as a shorthand using pre-existing JS patterns.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Typescript difference between two arrays

in typescript, What's the difference between these two index signature type?

What is the difference between the values of these two char arrays?

What is the difference between these two array assignments in Typescript?

What's the difference between 'extends' and 'implements' in TypeScript

What's the difference between String[] and [String] in typescript ?

What's the difference between ?: and | undefined in TypeScript?

How to display the difference between two arrays in Typescript/ JavaScript?

What's the difference between two QHeaderView signals?

What's the difference between these two types of parameters?

What's the difference between these two statements in Javascript?

What's the difference between these two dd commands?

What's the difference between these two memory allocations?

What's the semantical difference between the two statements?

What's the difference between these two queries?

What's the difference between these two addAction declaration?

What's the difference between these two printer icons?

What's the difference between the two closure

What's the difference between these two Scala patterns?

what's the difference between the two statements?

What's the difference between these two function declarations?

What's the difference between these two functions

What's the difference between these two code?

What's the difference between these two implementation?

What's difference between these two code snippets?

What is the difference between these two Enum(s) in Java

what's the difference between these two in C?

Cucumber - what's the difference between these two givens?

What's the difference between these two code fragments?