DEV Community

Cover image for Special kind of array in Typescript - Tuple

Special kind of array in Typescript - Tuple

Vikas yadav on September 26, 2021

In strictly typed programming languages, array is a Data structure of homogeneous data types with fixed length. In contrast JavaScript is dynamic. ...
Collapse
 
macsikora profile image
Pragmatic Maciej

Tuple is not Array in general definition. Tuple is implemented as Array in JS as there is no Tuple in the language. In Python(and many other) for example tuple syntax is different - tuple = (1,2) and array is arr = [1,2]. The title of this article is misleading.

Collapse
 
thejsdeveloper profile image
Vikas yadav

Thank you for pointing out. I have changed the title and included Typescript as well. This article is written in context of Typescript. Yes other languages like Python have different syntax. But in Typescript we have this syntax for now. As @urielsouza29 pointed out we have already a proposal for native support for Tuple in Javascript which will have a different syntax.

Collapse
 
totally_chase profile image
Phantz

A small mistake: arrays are not fixed length, not necessarily. Arrays in higher level type systems are usually dynamic in length. So the terms "array" and "vector" are often interchangeable.

But yes an array is a primitive homogenous collection. A tuple is a product type. Being a product type makes it have a constant number of different fields.

Collapse
 
jaspeling profile image
Johan

JavaScript/typescript is very dynamic. Which makes it very cool to do things like these - but I'm struggling to see where this will be beneficial? Having a class/interface with those properties on it is much easier to work with, pushing this type into a normal array. It also improves readability. Just because you can doesn't mean you should. I don't know - it's just my personal opinion. If you spend too much time trying to figure out what the previous developer did, you're already failing in writing maintainable code.

Collapse
 
thejsdeveloper profile image
Vikas yadav • Edited

Thanks for your valuable insight.

I partially agree with you @jaspeling . We can return a class/interface. But at the same time it is type introduced in Typescript. And it is very common while writing custom hooks to return an array as I explained in my example. In React useState uses same kind of pattern.

Collapse
 
jaspeling profile image
Johan

Ah, see I have no react knowledge. If that is a pattern used in a very well known framework, I guess I can see the value in a post like this.

Thread Thread
 
thejsdeveloper profile image
Vikas yadav

I see. Yes, it can be bit confusing at first. When I switched to react and saw this pattern I had the same feeling 😊

Collapse
 
elmuerte profile image
Michiel Hendriks • Edited

As if JavaScript hasn't done enough damage already... a tuple is not an array.

Collapse
 
urielsouza29 profile image
Uriel dos Santos Souza
Collapse
 
thejsdeveloper profile image
Vikas yadav

Thank you for the link @urielsouza29
:)

Collapse
 
thejsdeveloper profile image
Vikas yadav

Thanks for mentioning. Yes better DX :)