Skip to content

Primitives

string

keywords

🚧 Coming soon ™️🚧

literals
const 
const literals: Type<{
    singleQuoted: "typescript";
    doubleQuoted: "arktype";
}>
literals
=
const type: TypeParser
<{
    readonly singleQuoted: "'typescript'";
    readonly doubleQuoted: "\"arktype\"";
}, Type<{
    singleQuoted: "typescript";
    doubleQuoted: "arktype";
}, {}>>(def: validateObjectLiteral<{
    readonly singleQuoted: "'typescript'";
    readonly doubleQuoted: "\"arktype\"";
}, {}, bindThis<...>>) => Type<...> (+2 overloads)
type
({
singleQuoted: "'typescript'", doubleQuoted: '"arktype"' })
patterns

Regex literals specify an unanchored regular expression that an input string must match.

They can either be string-embedded or refer directly to a RegExp instance.

const 
const literals: Type<{
    stringEmbedded: string.matching<"^a.*z$">;
    regexLiteral: string.matching<string>;
}>
literals
=
const type: TypeParser
<{
    readonly stringEmbedded: "/^a.*z$/";
    readonly regexLiteral: RegExp;
}, Type<{
    stringEmbedded: string.matching<"^a.*z$">;
    regexLiteral: string.matching<string>;
}, {}>>(def: validateObjectLiteral<...>) => Type<...> (+2 overloads)
type
({
stringEmbedded: "/^a.*z$/", regexLiteral: /^a.*z$/ })
lengths

Constrain a string with an inclusive or exclusive min or max length.

const 
const bounded: Type<{
    nonEmpty: string.moreThanLength<0>;
    atLeastLength3: string.is<Nominal<"alphanumeric"> & AtLeastLength<3>>;
    lessThanLength10: string.lessThanLength<10>;
    atMostLength5: string.atMostLength<5>;
}>
bounded
=
const type: TypeParser
<{
    readonly nonEmpty: "string > 0";
    readonly atLeastLength3: "string.alphanumeric >= 3";
    readonly lessThanLength10: "string < 10";
    readonly atMostLength5: "string <= 5";
}, Type<{
    ...;
}, {}>>(def: validateObjectLiteral<...>) => Type<...> (+2 overloads)
type
({
nonEmpty: "string > 0", atLeastLength3: "string.alphanumeric >= 3", lessThanLength10: "string < 10", atMostLength5: "string <= 5" })

Range expressions allow you to specify both a min and max length and use the same syntax for exclusivity.

const 
const range: Type<{
    nonEmptyAtMostLength10: string.is<AtMostLength<10> & MoreThanLength<0>>;
    integerStringWith2To5Digits: string.is<Nominal<"integer"> & LessThanLength<6> & AtLeastLength<2>>;
}>
range
=
const type: TypeParser
<{
    readonly nonEmptyAtMostLength10: "0 < string <= 10";
    readonly integerStringWith2To5Digits: "2 <= string.integer < 6";
}, Type<{
    nonEmptyAtMostLength10: string.is<AtMostLength<10> & MoreThanLength<...>>;
    integerStringWith2To5Digits: string.is<...>;
}, {}>>(def: validateObjectLiteral<...>) => Type<...> (+2 overloads)
type
({
nonEmptyAtMostLength10: "0 < string <= 10", integerStringWith2To5Digits: "2 <= string.integer < 6" })

number

keywords

🚧 Coming soon ™️🚧

literals
const 
const literals: Type<{
    number: 1337;
}>
literals
=
const type: TypeParser
<{
    readonly number: "1337";
}, Type<{
    number: 1337;
}, {}>>(def: validateObjectLiteral<{
    readonly number: "1337";
}, {}, bindThis<{
    readonly number: "1337";
}>>) => Type<...> (+2 overloads)
type
({
number: "1337" })
ranges

Constrain a number with an inclusive or exclusive min or max.

const 
const bounded: Type<{
    positive: number.moreThan<0>;
    atLeast3: number.is<DivisibleBy<1> & AtLeast<3>>;
    lessThanPi: number.lessThan<3.14159>;
    atMost5: number.atMost<5>;
}>
bounded
=
const type: TypeParser
<{
    readonly positive: "number > 0";
    readonly atLeast3: "number.integer >= 3";
    readonly lessThanPi: "number < 3.14159";
    readonly atMost5: "number <= 5";
}, Type<{
    positive: number.moreThan<0>;
    atLeast3: number.is<...>;
    lessThanPi: number.lessThan<...>;
    atMost5: number.atMost<...>;
}, {}>>(def: validateObjectLiteral<...>) => Type<...> (+2 overloads)
type
({
positive: "number > 0", atLeast3: "number.integer >= 3", lessThanPi: "number < 3.14159", atMost5: "number <= 5" })

Range expressions allow you to specify both a min and max and use the same syntax for exclusivity.

const 
const range: Type<{
    positiveAtMostE: number.is<AtMost<2.71828> & MoreThan<0>>;
    evenNumberAbsoluteValueLessThan50: number.is<DivisibleBy<2> & LessThan<50> & MoreThan<-50>>;
}>
range
=
const type: TypeParser
<{
    readonly positiveAtMostE: "0 < number <= 2.71828";
    readonly evenNumberAbsoluteValueLessThan50: "-50 < (number % 2) < 50";
}, Type<{
    positiveAtMostE: number.is<AtMost<2.71828> & MoreThan<...>>;
    evenNumberAbsoluteValueLessThan50: number.is<...>;
}, {}>>(def: validateObjectLiteral<...>) => Type<...> (+2 overloads)
type
({
positiveAtMostE: "0 < number <= 2.71828", evenNumberAbsoluteValueLessThan50: "-50 < (number % 2) < 50" })
divisors

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" })

bigint

To allow any bigint value, use the "bigint" keyword.

const 
const bigints: Type<{
    foo: bigint;
}>
bigints
=
const type: TypeParser
<{
    readonly foo: "bigint";
}, Type<{
    foo: bigint;
}, {}>>(def: validateObjectLiteral<{
    readonly foo: "bigint";
}, {}, bindThis<{
    readonly foo: "bigint";
}>>) => Type<...> (+2 overloads)
type
({
foo: "bigint" })
literals

To require an exact bigint value in your type, you can use add the suffix n to a string-embedded number literal to make it a bigint.

const 
const literals: Type<{
    bigint: 1337n;
}>
literals
=
const type: TypeParser
<{
    readonly bigint: "1337n";
}, Type<{
    bigint: 1337n;
}, {}>>(def: validateObjectLiteral<{
    readonly bigint: "1337n";
}, {}, bindThis<{
    readonly bigint: "1337n";
}>>) => Type<...> (+2 overloads)
type
({
bigint: "1337n" })

You may also use a unit expression to define bigint literals.

symbol

To allow any symbol value, use the "symbol" keyword.

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

To reference a specific symbol in your definition, use a unit expression.

No special syntax is required to define symbolic properties like { [mySymbol]: "string" }. For more information and examples of how to combine symbolic keys with other syntax like optionality, see properties.

boolean

To allow true or false, use the "boolean" keyword.

const 
const booleans: Type<{
    key: boolean;
}>
booleans
=
const type: TypeParser
<{
    readonly key: "boolean";
}, Type<{
    key: boolean;
}, {}>>(def: validateObjectLiteral<{
    readonly key: "boolean";
}, {}, bindThis<{
    readonly key: "boolean";
}>>) => Type<...> (+2 overloads)
type
({
key: "boolean" })
literals

To require a specific boolean value, use the corresponding literal.

const 
const booleans: Type<{
    a: true;
    b: false;
    c: boolean;
}>
booleans
=
const type: TypeParser
<{
    readonly a: "true";
    readonly b: "false";
    readonly c: "true | false";
}, Type<{
    a: true;
    b: false;
    c: boolean;
}, {}>>(def: validateObjectLiteral<{
    readonly a: "true";
    readonly b: "false";
    readonly c: "true | false";
}, {}, bindThis<...>>) => Type<...> (+2 overloads)
type
({
a: "true", b: "false", // equivalent to the "boolean" keyword c: "true | false" })

null

The "null" keyword can be used to allow the exact value null, generally as part of a union.

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

undefined

The "undefined" keyword can be used to allow the exact value undefined, generally as part of a union.

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