Skip to content

parseTuple

Parses a value into a tuple matching a list of element guards. This parser uses the validateTuple structural validator to ensure that the input is an array of the correct length and that each element satisfies the corresponding guard.

The parser never throws and never mutates input. It returns a structured ParseResult<T> describing success or failure.

Signature

function parseTuple<
    const Guards extends readonly ((value: unknown) => boolean)[]
>(
    value: unknown,
    guards: Guards,
    field?: string
): ParseResult<{
    [I in keyof Guards]: Guards[I] extends (value: unknown) => value is infer T
        ? T
        : unknown;
}>

Behavior

  • Accepts arrays whose length matches the guard list.
  • Validates each element using the corresponding guard.
  • Rejects:
  • Non-arrays.
  • Arrays of incorrect length.
  • Mismatched element types.
  • Invalid guard functions.
  • Never mutates input.
  • Never throws.

Returns

  • ok: true: And the original tuple when parsing succeeds.
  • ok: false, value: null: And a list of issues when parsing fails.

Examples

parseTuple([1, "a"], [isNumber, isString])
// { ok: true, value: [1, "a"], issues: [] }

parseTuple([1], [isNumber, isString])
// { ok: false, value: null, issues: ["Value must be a tuple matching the specified structure"] }

Notes

  • This parser is foundational for schema-driven parsing.
  • It composes naturally with other parsers to build complex structured types.