Pair — Typed Pairs
In modern application code, data naturally comes in connected pairs: key-value entries in maps,
width and height dimensions, coordinate points (x, y), or HTTP response headers
(headerName, headerValue).
While TypeScript supports two-element array tuples (readonly [A, B]), treating them as raw arrays
inside pipeline compositions forces awkward destructuring and manual array rebuilding:
Pair<A, B> treats typed pairs (readonly [A, B]) as first-class, unified containers. It provides
dedicated combinators to transform the first element, the second element, or both elements
independently without breaking your pipeline flow.
Type Structure
Section titled “Type Structure”A Pair<A, B> is a structural alias for a readonly [A, B] array. Because it is a plain TypeScript
tuple under the hood, any native two-element array is automatically a valid Pair.
Pair provides dedicated, pure operations that manipulate this structure without requiring us to
destructure or manage array indices manually.
Creating Pairs
Section titled “Creating Pairs”To lift two distinct values into a typed pair, we use Pair.from.pair:
Any existing function that returns a two-element tuple (such as array utilities like Arr.splitAt)
is already structurally compatible with Pair and requires no constructor wrapping.
Inspecting and Accessing
Section titled “Inspecting and Accessing”We can extract elements from a pair using Pair.first and Pair.second, or reverse their order
using Pair.swap:
Transforming Pairs
Section titled “Transforming Pairs”Pair allows us to apply mapping functions to either element independently without affecting the
other, or transform both values simultaneously.
Transforming the first element with mapFirst
Section titled “Transforming the first element with mapFirst”Transforming the second element with mapSecond
Section titled “Transforming the second element with mapSecond”Transforming both elements with mapBoth
Section titled “Transforming both elements with mapBoth”Collapsing and Converting Pairs
Section titled “Collapsing and Converting Pairs”When we reach the end of a transformation pipeline and need to merge the pair into a single value,
we use Pair.fold. It applies a binary function that merges the two elements:
To run a side-effect (such as logging) in the middle of a pipeline without modifying the pair, use
Pair.tap. When interfacing with APIs that do not support tuple types, we can convert the pair to a
plain array using Pair.to.Array:
Problems it solves
Section titled “Problems it solves”- Selective element transformations in pipelines (
Pair.mapFirst,Pair.mapSecond,Pair.mapBoth): Transforming one half of a key-value entry or pair without breaking pipeline chain syntax or requiring temporary variables. - Key-value pair swapping for inverted indexes (
Pair.swap): When building inverse lookup indexes (such as converting map entries from IDs to usernames into usernames to IDs),Pair.swapflips pair positions cleanly inside array pipelines. - Collapsing paired data with
Pair.fold: Merging two values into a single formatted string or combined result without manual index accesses.