Signature
Description
Accepts a tuple (an array with exactly n items) of values accepted by the n given decoders. Unlikearray() which validates all elements with the same decoder, tuple() validates each position with a different decoder, enabling heterogeneous arrays with type safety.
Type Inference
The return type is a tuple type matching the decoder positions:Parameters
| Parameter | Type | Description |
|---|---|---|
...decoders | Decoder<unknown>[] | Variable number of decoders, one for each tuple position |
Returns
ADecoder<TupleDecoderType<Ds>> that validates a fixed-length array where each position matches its corresponding decoder.
Behavior
- Exact length required: Array must have exactly as many elements as decoders
- Position-specific validation: Each element validated by its corresponding decoder
- All positions validated: Even if one fails, all are checked to provide complete error information
- Wrong length: Decode fails with “Must be a N-tuple”
- Not an array: Decode fails with “Must be an array”
Examples
Basic Usage
Coordinates
Mixed Types
Nested Structures
Key-Value Pairs
Range Values
CSV Row Parsing
Versioned Data
Error Messages
The decoder provides detailed error messages:- Wrong length:
Must be a N-tuple- Example:
Must be a 3-tuple
- Example:
- Not an array:
Must be an array(from underlyingpojadecoder) - Element errors: Shows errors for all failed positions in the annotated array
Multiple Errors
Unlikearray(), tuple() collects errors from all positions:
Minimum Length
The TypeScript signature requires at least one decoder:Comparison with Array
| Feature | array(decoder) | tuple(...decoders) |
|---|---|---|
| Length | Variable | Fixed (exact match required) |
| Element types | Homogeneous (all same type) | Heterogeneous (different per position) |
| Type inference | T[] | [T1, T2, ..., Tn] |
| Error collection | Fails at first error | Collects all errors |
| Use case | Lists of items | Structured records |
Implementation Notes
- First validates the array length using
ntuple(n)refinement - Uses
.chain()to map over all decoders - Collects both successful values and errors
- Returns all results only if all decoders succeeded
- Provides annotated error array if any decoder failed
Related Decoders
array- Variable-length arrays with uniform typespoja- Accepts any array without validationnonEmptyArray- Arrays with at least one element
