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
🚧 Coming soon ™️🚧
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
🚧 Coming soon ™️🚧
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"
})
const const evens: Type<{
key: number.divisibleBy<2>;
}>
evens = const type: TypeParser
<{
readonly key: Type<number.divisibleBy<2>, {}>;
}, Type<{
key: number.divisibleBy<2>;
}, {}>>(def: validateObjectLiteral<{
readonly key: Type<number.divisibleBy<2>, {}>;
}, {}, bindThis<...>>) => 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)
})