Skip to content

tuple()

ts
function tuple<T>(parsers): Parser<T>

Construct parsers for tuples. If the data has more elements than expected, the extra elements are omitted from the result.

Type Parameters

Type Parameter
T extends readonly unknown[]

Parameters

ParameterTypeDescription
parsers[...{ [K in string | number | symbol]: Parser<T[K<K>]> }[]]an array of parsers. Each parser validates the corresponding element in the data tuple.

Returns

Parser<T>

a parser that validates tuples.

Examples

Parse 2D coordinates:

ts
const parseVector2 = tuple([parseNumber, parseNumber])
parseVector2([12.5, 45.0]) // -> ParseSuccess<[number, number]>

Declare the type explicitly with type arguments:

ts
const parseVector2 = tuple<[number, number]>([parseNumber, parseNumber])
parseVector2([12.5, 45.0]) // -> ParseSuccess<[number, number]>