#Is there any difference between Array<Foo> and Foo[] types or are they interchangeable?

12 messages · Page 1 of 1 (latest)

prisma crag
#

You can write interface Blah<T> extends Array<T> but you can't write interface Blah<T> extends T[]

prisma crag
#

Ever wonder what typescript's NodeArray is? 😄

woeful gulch
#

I'd say for most people it's a style thing. I prefer the "array-simple" style described here - https://typescript-eslint.io/rules/array-type/#array-simple - where [] is used only for single identfiers:

string[] // ✅
MyType[]; // ✅

Array<string | number>; // ✅
Array<{ x: string }> // ✅

(string | number)[]; // ❌
{ x: string }[]; // ❌
Array<string>; // ❌
prisma crag
#

Also, it's mandated on DT for any "complicated" type, because it may be the case that Array<string | number | object> is more readable than (string | number | object)[]

#

However, if it had been me designing the language, I would have instead used Go style ordering and had [](string | number | object); no more "it's this type and I'm reading and reading and surprise it was an array the whole time!"

woeful gulch
#

I also think "just use Array everywhere" is a decent style choice, but I like using [] where possible, but yeah, it can be obnoxious to read in complex types.

#

The "surprise it's array" is a big one (especially just a dangling [] at the end of a multi-line object definition is my favorite), the nesting also is kind of bad:

Foo<Bar<Baz>[]>>

is way harder to process than

Foo<Array<Bar<Baz>>>

(but also like... make a alias)

prisma crag
#

Foo<[]Bar<Baz>> 😭

woeful gulch
#

Huh, haven't actually seen the Go style before.

prisma crag
#

Yeah, it's pure left to right

#

none of this spiral rule crap