Skip to main content

namespace Tuple

A readonly array of fixed length and types.

Companion type: Tuple<T>

Functions

append

Returns the given tuple with the given values appended.

Definition

function append<T extends Tuple.Source, V extends readonly [unknown, ...unknown[]]>(tuple: T, ...values: V): readonly [...T, ...V];

Type parameters
NameDescription
T
V

Parameters

NameTypeDescription
tupleTthe source tuple
valuesVthe values to append
example
const t = Tuple.of(1, 'a')
console.log(Tuple.append(t, true, 5))
// => [1, 'a', true, 5]

concat

Returns a Tuple containing the elements of given tuple1 followed by the elements of given tuple2.

Definition

function concat<T1 extends Tuple.Source, T2 extends Tuple.Source>(tuple1: T1, tuple2: T2): readonly [...T1, ...T2];

Type parameters
NameDescription
T1
T2

Parameters

NameTypeDescription
tuple1T1the first Tuple
tuple2T2the second Tuple
example
const t1 = Tuple.of(1, 'a')
const t2 = Tuple.of(true, 5)
console.log(Tuple.concat(t1, t2))
// => [1, 'a', true, 5]

first

Returns the first element of a Tuple.

Definition

function first<T extends Tuple.Source>(tuple: T): T[0];

Type parameters
NameDescription
T

Parameters

NameTypeDescription
tupleTthe source tuple
example
const t = Tuple.of(1, 'a', true)
console.log(Tuple.first(t))
// => 1

getIndex

Returns the item at the given index in the givn tuple.

Definition

function getIndex<T extends Tuple.Source, K extends keyof T = keyof T>(tuple: T, index: K): T[K];

Type parameters
NameDescription
T
K

Parameters

NameTypeDescription
tupleTthe tuple to get the item from
indexKthe index in of the tuple element
example
const t = Tuple.of(1, 'a', true)
console.log(Tuple.getIndex(t, 1))
// => 'a'

init

Returns a Tuple containing all but the last element of the given tuple.

Definition

function init<T extends readonly unknown[]>(tuple: readonly [...T, unknown]): Readonly<T>;

Type parameters
NameDescription
T

Parameters

NameTypeDescription
tuplereadonly [...T, unknown]the source tuple
example
const t = Tuple.of(1, 'a', true)
console.log(Tuple.init(t))
// => [1, 'a']

last

Returns the last element of a Tuple.

Definition

function last<T extends readonly unknown[], R>(tuple: readonly [...T, R]): R;

Type parameters
NameDescription
T
R

Parameters

NameTypeDescription
tuplereadonly [...T, R]the source tuple
example
const t = Tuple.of(1, 'a', true)
console.log(Tuple.last(t))
// => true

of

Convenience method to type Tuple types

Definition

function of<T extends Tuple.NonEmptySource>(...values: T): Tuple<T>;

Type parameters
NameDescription
T

Parameters

NameTypeDescription
valuesTthe values of the tuple
example
const t = Tuple.of(1, 'a', true)
// type of t => Tuple<[number, string, boolean]>

second

Returns the second element of a Tuple.

Definition

function second<T extends Tuple.Source>(tuple: T): T[1];

Type parameters
NameDescription
T

Parameters

NameTypeDescription
tupleTthe source tuple
example
const t = Tuple.of(1, 'a', true)
console.log(Tuple.second(t))
// => 'a'

tail

Returns a Tuple containing all but the first element of the given tuple.

Definition

function tail<T extends readonly [...unknown[]]>(tuple: readonly [unknown, ...T]): Readonly<T>;

Type parameters
NameDescription
T

Parameters

NameTypeDescription
tuplereadonly [unknown, ...T]the source tuple
example
const t = Tuple.of(1, 'a', true)
console.log(Tuple.tail(t))
// => ['a', true]

updateAt

Returns a copy of the given tuple where the element at given index is updated with the given updater.

Definition

function updateAt<T extends Tuple.Source, K extends keyof T = keyof T>(tuple: T, index: K, updater: Update<T[K]>): T;

Type parameters
NameDescription
T
K

Parameters

NameTypeDescription
tupleTthe source tuple
indexKthe index in the tuple
updaterUpdate<T[K]>the updater for the value
example
const t = Tuple.of(1, 'a', true)
console.log(Tuple.updateAt(t, 1, 'b'))
// => [1, 'b', true]