BRIEFS logo BRIEFS

TypeScript: Why I prefer `Array<Type>` over `Type[]`

05/19/2021 , 2m, 45s

Good morning or afternoon or whatever it is for you friends, it's morning for me. I wanted to talk about how I type a raise in TypeScript, so there are two ways that I'm aware of that you can type an array and of course I guess there are a couple ways depending on what type of an array you want to make but just like a regular array you can either specify the type and then immediately thereafter with no space a open and closing bracket like the the syntax for an array and the alternative is there is a built-in.

Array type which is a generic and you say array and then open bracket no that what are these angle brackets that's what is called an open angle bracket and then the type of that's in the array and then a closing angle bracket and I prefer the latter. I like using the generic array and the reason that I prefer that I I think that I just like just kind of preferred to that from the beginning, but I think the reason that I prefer using the generic array is because I don't,Like reading my type and then getting to the end and be like, oh this is an array of those things so I I see we've got um user and then I see the opening closing bracket and I say oh no, actually it's not a user it's an array of users, whereas if I use the generic I can see oh we've got an array and the things that are in the array are users and so I'm when I'm reading from left to right it just seems to make more sense to me to to read it in that way, it's kind of like in English it.

I I like how in Spanish and you have the,If you have like an exclamation you're going to have that upset upside down exclamation point I don't know what that's called but you have that upside down exclamation point at the beginning of the sentence so you know ahead of time that the thing you're about to read is an exclamation rather than getting to the end of the sentence and being oh yeah, I'm supposed to be like excited about in saying this or something like that when I'm reading a book my kid or something and so I kind of see that in the same way and that's why I prefer using the generic over the the special syntax for specifying arrays, it is a little bit more type.

Of being or more characters but I actually don't care too much about that and then that comes with the benefit of switching from a regular array to a read-only array, you know being a bit easier and stuff like that so anyway. I just thought that would be something I'd share today.

I hope you're having a wonderful day and I'll chat with you soon.