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"]
}
])
const const myTuple: Type<[string, {
coordinates: [number, number];
}]>
myTuple = const type: TypeParser
<readonly [Type<string, {}>, {
readonly coordinates: readonly [Type<number, {}>, Type<number, {}>];
}], Type<[string, {
coordinates: [number, number];
}], {}>>(def: readonly [Type<string, {}>, validateObjectLiteral<...>]) => Type<...> (+2 overloads)
type([
type.string,
// Object definitions can be nested in tuples- and vice versa!
{
coordinates: [type.number, type.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?"])
const const myTuple: Type<[string, boolean?, number?]>
myTuple = const type: TypeParser
<readonly [Type<string, {}>, Type<of<boolean, Optional>, {}>, Type<...>], Type<...>>(def: readonly [...]) => Type<...> (+2 overloads)
type([
type.string,
type.boolean.optional(),
type.number.optional()
])
const const myTuple: Type<[string, {
name: string;
}?]>
myTuple = const type: TypeParser
<readonly ["string", readonly [{
readonly name: "string";
}, "?"]], Type<[string, {
name: string;
}?], {}>>(def: readonly ["string", readonly [{
readonly name: "string";
}, "?"]]) => Type<[string, {
name: string;
}?], {}> (+2 overloads)
type([
"string",
[
{
name: "string"
},
"?"
]
])
const const myTuple: Type<[string, {
name: string;
}?]>
myTuple = const type: TypeParser
<readonly ["string", Type<of<{
name: string;
}, Optional>, {}>], Type<[string, {
name: string;
}?], {}>>(def: readonly ["string", Type<of<{
name: string;
}, Optional>, {}>]) => Type<...> (+2 overloads)
type([
"string",
const type: TypeParser
<{
readonly name: "string";
}, "?", readonly [], Type<of<{
name: string;
}, Optional>, {}>>(_0: validateObjectLiteral<{
readonly name: "string";
}, {}, bindThis<{
readonly name: "string";
}>>, _1: "?") => Type<...> (+2 overloads)
type(
{
name: "string"
},
"?"
)
])
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[]"])
// allows a string followed by zero or more numbers
const const myTuple: Type<[string, ...number[]]>
myTuple = const type: TypeParser
<readonly [Type<string, {}>, "...", Type<number[], {}>], Type<[string, ...number[]], {}>>(def: readonly [Type<...>, "...", Type<...>]) => Type<...> (+2 overloads)
type([type.string, "...", type.number.array()])
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"])
// allows zero or more numbers followed by a boolean, then a string
const const myTuple: Type<[...number[], boolean, string]>
myTuple = const type: TypeParser
<readonly ["...", Type<number[], {}>, Type<boolean, {}>, Type<string, {}>], Type<[...number[], boolean, string], {}>>(def: readonly [...]) => Type<...> (+2 overloads)
type(["...", type.number.array(), type.boolean, type.string])