Skip to content

Tuples

Like objects, tuples are structures whose values are nested definitions. Like TypeScript, ArkType supports prefix, optional, variadic, and postfix elements, with the same restrictions about combining them.

Prefix Elements

const 
const myTuple: Type<[string, {
    coordinates: [number, number];
}]>
myTuple
=
const type: TypeParser
<readonly ["string", {
    readonly coordinates: readonly ["number", "number"];
}], Type<[string, {
    coordinates: [number, number];
}], {}>>(def: readonly ["string", validateObjectLiteral<{
    readonly coordinates: readonly ["number", "number"];
}, {}, bindThis<...>>]) => Type<...> (+2 overloads)
type
([
"string", // Object definitions can be nested in tuples- and vice versa! { coordinates: ["number", "number"] } ])

Optional Elements

Tuples can include any number of optional elements following its prefix elements.

Like in TypeScript, optional elements are mutually exclusive with postfix elements.

const const myTuple: Type<[string, boolean?, number?]>myTuple = 
const type: TypeParser
<readonly ["string", "boolean?", "number?"], Type<[string, boolean?, number?], {}>>(def: readonly ["string", "boolean?", "number?"]) => Type<[string, boolean?, number?], {}> (+2 overloads)
type
(["string", "boolean?", "number?"])

Variadic Elements

Like in TypeScript, variadic elements allow zero or more consecutive values of a given type and may occur at most once in a tuple.

They are specified with a "..." operator preceding an array element.

// allows a string followed by zero or more numbers
const const myTuple: Type<[string, ...number[]]>myTuple = 
const type: TypeParser
<readonly ["string", "...", "number[]"], Type<[string, ...number[]], {}>>(def: readonly ["string", "...", "number[]"]) => Type<[string, ...number[]], {}> (+2 overloads)
type
(["string", "...", "number[]"])

Postfix Elements

Postfix elements are required elements following a variadic element.

They are mutually exclusive with optional elements.

// allows zero or more numbers followed by a boolean, then a string
const const myTuple: Type<[...number[], boolean, string]>myTuple = 
const type: TypeParser
<readonly ["...", "number[]", "boolean", "string"], Type<[...number[], boolean, string], {}>>(def: readonly ["...", "number[]", "boolean", "string"]) => Type<[...number[], boolean, string], {}> (+2 overloads)
type
(["...", "number[]", "boolean", "string"])