423

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

4
  • 5
    Possible duplicate of Typescript Array vs any[] Commented Apr 25, 2016 at 13:39
  • 1
    They are practically the same constructs, and are effectively the same objects at runtime. Reflect-metadata will also treat them both as having the Array object as their constructor. See the above linked answer. Commented Apr 25, 2016 at 13:43
  • 5
    @NitzanTomer that question is out-of-date -- Array<T> didn't exist back then. Commented Apr 25, 2016 at 17:14
  • @NathanShively-Sanders the answer is the same with or without the generics because this question can be reduced to "what's the different between let x: Array; to let x: any[];" Commented Apr 25, 2016 at 17:25

2 Answers 2

473

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>;
Sign up to request clarification or add additional context in comments.

12 Comments

@dragonmnl, It is a generic type. Just read the section "Hello World of generics" in the Handbook.
There isn't an official recommendation. I personally use the shorthand and only the shorthand (type[]), because it is easier to read.
They aren't completely identical anymore: typescriptlang.org/docs/handbook/release-notes/…
If I got it right, in readonly case it's not actually required. If you prefer a long notation you can write it like this: let err2: ReadonlyArray<boolean>;
@n4nn31355 I think so. I edited to add this notice.
|
6

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

2 Comments

Its also called tuples typescriptlang.org/docs/handbook/2/objects.html#tuple-types was going to write that its not a fixed size array, but it actually is, still definition is IMO misleading. Fixed size array would look like const arr = new Array<number>(3);, though technically it will not be fixed ofc
The "fixed length arrays" are an entirely different feature. So, no there is no difference when using the actual array type syntax. You're describing Difference between type[] and [type] in TypeScript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.