Position-specific data
Representing structured records encoded as arrays, such as [name, identifier, active].
Zod · Function
z.tuple defines a tuple schema whose positions can use different Zod schemas. An optional rest schema allows additional values after the fixed prefix.
Use z.tuple to declare an array whose positions have distinct schemas. Zod validates the required items in order and infers a corresponding TypeScript tuple; passing a second schema adds a variadic rest portion after the fixed items.
z.tuple(items, rest?)| Name | Type | Description |
|---|---|---|
items | ZodType[] | Array of schemas for the tuple's fixed positional elements. |
rest | ZodType | Optional schema applied to additional elements after the fixed items. |
A Zod tuple schema that validates fixed positional elements and, when supplied, additional elements against the rest schema.
import { z } from "zod";
const UserTuple = z.tuple([z.string(), z.number()]);
const user = UserTuple.parse(["Ada", 37]);
console.log(user); // ["Ada", 37]Defines and validates a two-element tuple containing a string followed by a number.
Representing structured records encoded as arrays, such as [name, identifier, active].
Accepting a required prefix followed by zero or more values of one type.
Deriving a precise TypeScript tuple type from runtime validation rules.
z.tuple accepts an ordered list of schemas, with each schema validating the value at its corresponding index. Unlike an array schema, it models a fixed positional structure and infers a TypeScript tuple.
Follow-up: How would you allow additional values? Pass a second schema, such as z.tuple([z.string()], z.number()), to model a string followed by zero or more numbers.
The first argument defines the fixed prefix. An optional second schema defines the variadic portion, producing an inferred type like [string, ...number[]].
Follow-up: What is the difference from z.array(z.number())? The tuple preserves the required first position and permits only numbers in subsequent positions.
Each position can use a different schema, so z.tuple([z.string(), z.number(), z.boolean()]) represents [string, number, boolean]. Parsing checks both the order and the value schemas.
Follow-up: How can the resulting tuple be made immutable at the type level? Apply .readonly(), which yields a readonly tuple type.
z.arrayz.inferz.readonlyz.union