Skip to main content

Paul Armstrong’s profile picture Paul Armstrong

There can be only one Array<T>

This is that one little syntax hill that I will forever defend. The shorthand syntax for Arrays in TypeScript is unnecessary and unclear.

Proper Array syntax is good
Array<T>
Shorthand Array syntax is bad
T[]

Why you should use the Array type

  • Immediately clear when reading left-to-right that the type is an Array.

    type MyStuff = Array<{ some: BigObject }>;
  • Matches the syntax for Set<T> and Map<K, V>

    type MyArr = Array<T>;
    type MySet = Set<T>;
    type MyMap = Map<K, V>;

Why you should not use Array shorthand

  • Easily overused on complex types

    type MyStuff = { some: BigObject }[];

    You’re probably going to argue that { some: BigObject } should be further extracted out, and you’re right, but remember that people are inherently lazy and won’t always do that.

  • Easily mistaken between tuple types, but they are not identical:

    [string] !== string[]

Automate the good syntax

Arguably, it’s hard to enforce a single way when two different syntaxes have the exact same meaning. Luckily, there’s an auto-fixing ESLint rule for that: Use @typescript-eslint/array-type set to "generic":

.eslintrc.cjs
module.exports = {
rules: {
'@typescript-eslint/array-type': ['error', { default: 'generic', readonly: 'generic' }],
},
};