Tuple / Pair / Triple structures

Author: astorijeCreated Apr 8, 2015Updated Jun 17, 2026
Labelsenhancement

Disclaimer: I am using JavaScript only, so I don't know the potential specifics of TypeScript for this topic

Scala defines Tuple structures, from Tuple1 to... Tuple22!

Along the way, Tuple2 and Tuple3 are pretty nice, they come with extra methods (like swap for Tuple2. These two are so common that they respectively are aliased to Pair and Triple (were actually, since they got deprecated in favor of a syntax we unfortunately cannot use here).

Right now, if I want to make a pair, I can either use an Array (and therefore be mutable) or a List (and therefore abuse the types):

javascript
var a = ['foo', 42];
var l = List.of('foo', 42);

Isn't the second one a violation of the types?

I think it'd be nicer to have specific data structures like:

javascript
var p = new Pair('foo', 42);
var t = new Tuple2('foo', 42);

Among other things, a nice consequence of this, for those who are using JavaScript and JSDoc is that the former version has to be documented with {List.<*>}, while the latter one can be documented with {Pair.<string, number>}, which is much more precise and useful documentation!

Am I completely missing the point here?

Source: immutable-js/immutable-js