#Is there any difference between Array<Foo> and Foo[] types or are they interchangeable?
12 messages · Page 1 of 1 (latest)
TIL.
Ever wonder what typescript's NodeArray is? 😄
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>; // ❌
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!"
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)
Foo<[]Bar<Baz>> 😭
Huh, haven't actually seen the Go style before.