Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array<Type> VS Type[] in Typescript

Tags:

typescript

As far as I know a property's type can be defined in two ways when it's an Array.

property_name: type

where type can be either

Array<string>, Array<MyType>, etc. (e.g. let prop1: Array<string>)

and

string[], MyType[], etc. (e.g. let prop1: string[])

What is the difference between the two cases? Or am I misunderstanding something (perhaps something about <> used in casting?)

EDIT since the question is marked as duplicate, I am aware there is the other question about any[] but still I had a look at it before posting and to me it was more about the type 'any' than the different [] VS <> I asked

like image 412
dragonmnl Avatar asked Apr 25 '16 13:04

dragonmnl


People also ask

What is string [] in TypeScript?

In TypeScript, the string is sequence of char values and also considered as an object. It is a type of primitive data type that is used to store text data. The string values are used between single quotation marks or double quotation marks, and also array of characters works same as a string.

Is string [] the same as array string?

There's no difference between the two, it's the same. It says this in the docs: Array types can be written in one of two ways.

What is array type in TypeScript?

In typescript, an array is a data type that can store multiple values of different data types sequentially. Similar to JavaScript, Typescript supports array declaration and there are multiple ways to do it. Declaring and Initializing Arrays: We can either use var or let for declaring an array.

How do you declare an array of any type in TypeScript?

In TypeScript, an array is an ordered list of values. An array can store a mixed type of values. To declare an array of a specific type, you use the let arr: type[] syntax.


2 Answers

There isn't any semantic difference

There is no difference at all. Type[] is the shorthand syntax for an array of Type. Array<Type> is the generic syntax. They are completely equivalent.

The handbook provides an example here. It is equivalent to write:

function loggingIdentity<T>(arg: T[]): T[] {
    console.log(arg.length);
    return arg;
}

Or:

function loggingIdentity<T>(arg: Array<T>): Array<T> {
    console.log(arg.length);
    return arg;
}

And here is a quote from some release notes:

Specifically, number[] is a shorthand version of Array<number>, just as Date[] is a shorthand for Array<Date>.

About the readonly type modifier

TypeScript 3.4, introduces the readonly type modifier. With a precision:

the readonly type modifier can only be used for syntax on array types and tuple types

let err2: readonly Array<boolean>; // error!    
let okay: readonly boolean[]; // works fine

The following declaration is equivalent to readonly boolean[]:

let okay2: ReadonlyArray<boolean>;
like image 199
Paleo Avatar answered Oct 09 '22 20:10

Paleo


There is a difference when you are defining fixed length arrays. You can't define a fixed length array with Array<>, you have to use the shorthand syntax:

const myFunc1 = (arg: [string, number, boolean]) => {
  console.log(arg);
};
myFunc1(["hello world", 123, true]);

// error: TS2314: Generic type 'Array ' requires 1 type argument(s).
const myFunc2 = (arg: Array<string, number, boolean>) => {
  console.log(arg);
};
myFunc2(["hello world", 123, true])

like image 5
etoxin Avatar answered Oct 09 '22 21:10

etoxin