Skip to content

Expressions

Array

const 
const arrays: Type<{
    key: string[];
}>
arrays
=
const type: TypeParser
<{
    readonly key: "string[]";
}, Type<{
    key: string[];
}, {}>>(def: validateObjectLiteral<{
    readonly key: "string[]";
}, {}, bindThis<{
    readonly key: "string[]";
}>>) => Type<...> (+2 overloads)
type
({
key: "string[]" })

Divisibility

Constrain a number to a multiple of the specified integer.

const 
const evens: Type<{
    key: number.divisibleBy<2>;
}>
evens
=
const type: TypeParser
<{
    readonly key: "number%2";
}, Type<{
    key: number.divisibleBy<2>;
}, {}>>(def: validateObjectLiteral<{
    readonly key: "number%2";
}, {}, bindThis<{
    readonly key: "number%2";
}>>) => Type<...> (+2 overloads)
type
({
key: "number%2" })

Equality

While embedded literal syntax is usually ideal for defining exact primitive values, the === operator, type.unit and type.enumerated can be helpful for defining a non-serializable reference like a symbol or for deriving a type from a pre-existing list of allowed values.

const const mySymbol: typeof mySymbolmySymbol = Symbol()

const const exactValue: Type<typeof mySymbol>exactValue = type.unit(const mySymbol: typeof mySymbolmySymbol)

const const exactValueFromSet: Type<true | 1337 | null>exactValueFromSet = type.enumerated(1337, true, null)

Parenthetical

By default, ArkType’s operators follow the same precedence as TypeScript’s. Also like in TypeScript, this can be overridden by wrapping an expression in parentheses.

// hover to see the distinction!
const 
const groups: Type<{
    stringOrArrayOfNumbers: string | number[];
    arrayOfStringsOrNumbers: (string | number)[];
}>
groups
=
const type: TypeParser
<{
    readonly stringOrArrayOfNumbers: "string | number[]";
    readonly arrayOfStringsOrNumbers: "(string | number)[]";
}, Type<{
    stringOrArrayOfNumbers: string | number[];
    arrayOfStringsOrNumbers: (string | number)[];
}, {}>>(def: validateObjectLiteral<...>) => Type<...> (+2 overloads)
type
({
stringOrArrayOfNumbers: "string | number[]", arrayOfStringsOrNumbers: "(string | number)[]" })

instanceof

Most builtin instance types like Array and Date are available directly as keywords, but instanceof can be useful for constraining a type to one of your own classes.

class MyClass {}

const const instances: Type<MyClass>instances = type.instanceOf(MyClass)

Union

All unions are automatically discriminated to optimize check time and error message clarity.

const 
const unions: Type<{
    key: string | number;
}>
unions
=
const type: TypeParser
<{
    readonly key: "string | number";
}, Type<{
    key: string | number;
}, {}>>(def: validateObjectLiteral<{
    readonly key: "string | number";
}, {}, bindThis<{
    readonly key: "string | number";
}>>) => Type<...> (+2 overloads)
type
({
key: "string | number" })