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;
regexLiteral: string;
}>
literals = const type: TypeParser
<{
readonly stringEmbedded: "/^a.*z$/";
readonly regexLiteral: RegExp;
}, Type<{
stringEmbedded: string;
regexLiteral: string;
}, {}>>(def: validateObjectLiteral<{
readonly stringEmbedded: "/^a.*z$/";
readonly regexLiteral: RegExp;
}, {}, bindThis<...>>) => 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;
atLeastLength3: string;
lessThanLength10: string;
atMostLength5: string;
}>
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"
})
const const bounded: Type<{
nonEmpty: string;
atLeastLength3: string;
lessThanLength10: string;
atMostLength5: string;
}>
bounded = const type: TypeParser
<{
readonly nonEmpty: Type<string, {}>;
readonly atLeastLength3: Type<string, Ark>;
readonly lessThanLength10: Type<string, {}>;
readonly atMostLength5: Type<string, {}>;
}, Type<...>>(def: validateObjectLiteral<...>) => Type<...> (+2 overloads)
type({
nonEmpty: type.string.moreThanLength(0),
atLeastLength3: type.keywords.string.alphanumeric.atLeastLength(3),
lessThanLength10: type.string.lessThanLength(10),
atMostLength5: type.string.atMostLength(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;
integerStringWith2To5Digits: string;
}>
range = const type: TypeParser
<{
readonly nonEmptyAtMostLength10: "0 < string <= 10";
readonly integerStringWith2To5Digits: "2 <= string.integer < 6";
}, Type<{
nonEmptyAtMostLength10: string;
integerStringWith2To5Digits: string;
}, {}>>(def: validateObjectLiteral<...>) => Type<...> (+2 overloads)
type({
nonEmptyAtMostLength10: "0 < string <= 10",
integerStringWith2To5Digits: "2 <= string.integer < 6"
})
const const range: Type<{
nonEmptyAtMostLength10: string;
integerStringWith2To5Digits: string;
}>
range = const type: TypeParser
<{
readonly nonEmptyAtMostLength10: Type<string, {}>;
readonly integerStringWith2To5Digits: Type<string, Ark>;
}, Type<{
nonEmptyAtMostLength10: string;
integerStringWith2To5Digits: string;
}, {}>>(def: validateObjectLiteral<...>) => Type<...> (+2 overloads)
type({
nonEmptyAtMostLength10: type.string.moreThanLength(0).atMostLength(10),
integerStringWith2To5Digits: type.keywords.string.integer.root
.atLeastLength(2)
.lessThanLength(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;
atLeast3: number;
lessThanPi: number;
atMost5: number;
}>
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;
atLeast3: number;
lessThanPi: number;
atMost5: number;
}, {}>>(def: validateObjectLiteral<...>) => Type<...> (+2 overloads)
type({
positive: "number > 0",
atLeast3: "number.integer >= 3",
lessThanPi: "number < 3.14159",
atMost5: "number <= 5"
})
const const bounded: Type<{
positive: number;
atLeast3: number;
lessThanPi: number;
atMost5: number;
}>
bounded = const type: TypeParser
<{
readonly positive: Type<number, {}>;
readonly atLeast3: Type<number, Ark>;
readonly lessThanPi: Type<number, {}>;
readonly atMost5: Type<number, {}>;
}, Type<...>>(def: validateObjectLiteral<...>) => Type<...> (+2 overloads)
type({
positive: type.number.moreThan(0),
atLeast3: type.keywords.number.integer.atLeast(3),
lessThanPi: type.number.lessThan(3.14159),
atMost5: type.number.atMost(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;
evenNumberAbsoluteValueLessThan50: number;
}>
range = const type: TypeParser
<{
readonly positiveAtMostE: "0 < number <= 2.71828";
readonly evenNumberAbsoluteValueLessThan50: "-50 < (number % 2) < 50";
}, Type<{
positiveAtMostE: number;
evenNumberAbsoluteValueLessThan50: number;
}, {}>>(def: validateObjectLiteral<...>) => Type<...> (+2 overloads)
type({
positiveAtMostE: "0 < number <= 2.71828",
evenNumberAbsoluteValueLessThan50: "-50 < (number % 2) < 50"
})
const const range: Type<{
positiveAtMostE: number;
evenNumberAbsoluteValueLessThan50: number;
}>
range = const type: TypeParser
<{
readonly positiveAtMostE: Type<number, {}>;
readonly evenNumberAbsoluteValueLessThan50: Type<number, {}>;
}, Type<{
positiveAtMostE: number;
evenNumberAbsoluteValueLessThan50: number;
}, {}>>(def: validateObjectLiteral<...>) => Type<...> (+2 overloads)
type({
positiveAtMostE: type.number.moreThan(0).atMost(2.71828),
evenNumberAbsoluteValueLessThan50: type.number
.divisibleBy(2)
.moreThan(-50)
.lessThan(50)
})
divisors
Constrain a number
to a multiple of the specified integer.
const const evens: Type<{
key: number;
}>
evens = const type: TypeParser
<{
readonly key: "number % 2";
}, Type<{
key: number;
}, {}>>(def: validateObjectLiteral<{
readonly key: "number % 2";
}, {}, bindThis<{
readonly key: "number % 2";
}>>) => Type<...> (+2 overloads)
type({
key: "number % 2"
})
const const evens: Type<{
key: number;
}>
evens = const type: TypeParser
<{
readonly key: Type<number, {}>;
}, Type<{
key: number;
}, {}>>(def: validateObjectLiteral<{
readonly key: Type<number, {}>;
}, {}, bindThis<{
readonly key: Type<number, {}>;
}>>) => Type<...> (+2 overloads)
type({
key: type.number.divisibleBy(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"
})
const const symbols: Type<{
foo: bigint;
}>
symbols = const type: TypeParser
<{
readonly foo: Type<bigint, {}>;
}, Type<{
foo: bigint;
}, {}>>(def: validateObjectLiteral<{
readonly foo: Type<bigint, {}>;
}, {}, bindThis<{
readonly foo: Type<bigint, {}>;
}>>) => Type<...> (+2 overloads)
type({
foo: type.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"
})
const const symbols: Type<{
key: symbol;
}>
symbols = const type: TypeParser
<{
readonly key: Type<symbol, {}>;
}, Type<{
key: symbol;
}, {}>>(def: validateObjectLiteral<{
readonly key: Type<symbol, {}>;
}, {}, bindThis<{
readonly key: Type<symbol, {}>;
}>>) => Type<...> (+2 overloads)
type({
key: type.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"
})
const const booleans: Type<{
key: boolean;
}>
booleans = const type: TypeParser
<{
readonly key: Type<boolean, {}>;
}, Type<{
key: boolean;
}, {}>>(def: validateObjectLiteral<{
readonly key: Type<boolean, {}>;
}, {}, bindThis<{
readonly key: Type<boolean, {}>;
}>>) => Type<...> (+2 overloads)
type({
key: type.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"
})
const const booleans: Type<{
a: true;
b: false;
c: boolean;
}>
booleans = const type: TypeParser
<{
readonly a: Type<true, Ark>;
readonly b: Type<false, Ark>;
readonly c: Type<boolean, Ark>;
}, Type<{
a: true;
b: false;
c: boolean;
}, {}>>(def: validateObjectLiteral<{
readonly a: Type<true, Ark>;
readonly b: Type<false, Ark>;
readonly c: Type<boolean, Ark>;
}, {}, bindThis<...>>) => Type<...> (+2 overloads)
type({
a: type.keywords.true,
b: type.keywords.false,
// equivalent to the "boolean" keyword
c: type.keywords.true.or(type.keywords.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"
})
const const nullable: Type<{
requiredKey: number | null;
}>
nullable = const type: TypeParser
<{
readonly requiredKey: Type<number | null, {}>;
}, Type<{
requiredKey: number | null;
}, {}>>(def: validateObjectLiteral<{
readonly requiredKey: Type<number | null, {}>;
}, {}, bindThis<...>>) => Type<...> (+2 overloads)
type({
requiredKey: type.number.or(type.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"
})
const const myObj: Type<{
requiredKey: number | undefined;
}>
myObj = const type: TypeParser
<{
readonly requiredKey: Type<number | undefined, {}>;
}, Type<{
requiredKey: number | undefined;
}, {}>>(def: validateObjectLiteral<{
readonly requiredKey: Type<number | undefined, {}>;
}, {}, bindThis<...>>) => Type<...> (+2 overloads)
type({
requiredKey: type.number.or(type.undefined)
})