‹

Preferred Array Type Notation

Array<string> versus string[]

My preferred way of writing an Array type is Array<string> and not the shorthand string[] and there are two short, simple reasons: Readability and Consistency.

Readability

The shorthand string[] is shorter than Array<string> and might in a codebase reduce visual noise.

const fruits: string[] = [ "Apples", "Oranges" ];
const vegetables: Array<string> = [ "Broccoli", "Brussels Sprouts" ];

A compiler reads them the same, but a developer reads them in two different ways: Array<string> is read from left to right, and string[] is read from right to left. "An Array of strings" versus "Strings in an Array". This difference is negligible for simple types, but becomes more noticeable on longer and more complex types.

const nerds: { id: number, name: string, hobbies: string[] }[] = [];
const geeks: Array<{ id: number, name: string, hobbies: Array<string> }> = [];

When reading the type for the nerds constant, you're reading an object and only at the end do you notice it's actually an Array.

When reading the type for the geeks constant, you immediately know the outer type is an Array.

We read code top down, from left to right; reading a type should be in the same cadence.

Consistency

Array is the only type that has a shorthand, other generic types don't. There's Map<T>, Set<T>, and Promise<T> amongst many others. Personally, I like things to be consistent.

Conclusion

It's a personal preference in the end. TypeScript's own documentation and frankly most examples I see only lean towards the shorthand. I find the Readability and Consistency arguments conclusive enough to go with the longhand form.

The choice starts to weigh more the larger the codebase gets and the more developers working on the project. Different people with different preferences on the same codebase gets messy fast. That's where it stops being about which form is "better" and becomes about picking one and enforcing it — the value is in the consistency, not the choice itself.