Primitives
Define strings, numbers, bigints, symbols, booleans, null and undefined, with literals, patterns, lengths, ranges and divisors.
string
keywords
Reference a keyword anywhere a definition is expected:
const const User: Type<{
email: string;
ids: string[];
}>
User = type({
email: "string.email",
ids: "string.uuid.v4[]"
})| keyword | description |
|---|---|
string | a string |
string.alpha | only letters |
string.alphanumeric | only letters and digits 0-9 |
string.base58 | base58-encoded |
string.base64 | base64-encoded |
string.base64.url | base64url-encoded |
string.boolean | "false" or "true" |
string.boolean.parse | a morph from "false" or "true" to boolean |
string.capitalize | a morph from a string to capitalized |
string.capitalize.preformatted | capitalized |
string.creditCard | a credit card number |
string.date | a parsable date |
string.date.epoch | an integer string representing a safe Unix timestamp |
string.date.epoch.parse | a morph from an integer string representing a safe Unix timestamp to a Date |
string.date.iso | an ISO 8601 date or date-time |
string.date.iso.date | an ISO 8601 date |
string.date.iso.parse | a morph from an ISO 8601 date or date-time to a Date |
string.date.parse | a morph from a parsable date to a Date |
string.digits | only digits 0-9 |
string.email | an email address |
string.hex | hex characters only |
string.integer | a well-formed integer string |
string.integer.parse | a morph from a well-formed integer string to an integer and at least -9007199254740991 and at most 9007199254740991 |
string.ip | an IP address |
string.ip.v4 | an IPv4 address |
string.ip.v6 | an IPv6 address |
string.json | a JSON string |
string.json.parse | a JSON string |
string.lower | a morph from a string to lowercase |
string.lower.preformatted | lowercase |
string.nonblank | non-blank |
string.normalize | a morph from a string to NFC-normalized unicode |
string.normalize.NFC | a morph from a string to NFC-normalized unicode |
string.normalize.NFC.preformatted | NFC-normalized unicode |
string.normalize.NFD | a morph from a string to NFD-normalized unicode |
string.normalize.NFD.preformatted | NFD-normalized unicode |
string.normalize.NFKC | a morph from a string to NFKC-normalized unicode |
string.normalize.NFKC.preformatted | NFKC-normalized unicode |
string.normalize.NFKD | a morph from a string to NFKD-normalized unicode |
string.normalize.NFKD.preformatted | NFKD-normalized unicode |
string.numeric | a well-formed numeric string |
string.numeric.parse | a morph from a well-formed numeric string and finite to a number |
string.regex | a regex pattern |
string.semver | a semantic version (see https://semver.org/) |
string.trim | a morph from a string to trimmed |
string.trim.preformatted | trimmed |
string.upper | a morph from a string to uppercase |
string.upper.preformatted | uppercase |
string.url | a URL string |
string.url.http | an HTTP or HTTPS URL |
string.url.parse | a morph from a URL string to a URL instance |
string.uuid | a UUID |
string.uuid.v1 | a UUIDv1 |
string.uuid.v2 | a UUIDv2 |
string.uuid.v3 | a UUIDv3 |
string.uuid.v4 | a UUIDv4 |
string.uuid.v5 | a UUIDv5 |
string.uuid.v6 | a UUIDv6 |
string.uuid.v7 | a UUIDv7 |
string.uuid.v8 | a UUIDv8 |
Every keyword, including those for other types, is listed on the keywords page.
literals
Quote a string inside a definition to allow exactly that string. Single and double quotes are equivalent:
const const Literals: Type<{
singleQuoted: "typescript";
doubleQuoted: "arktype";
}>
Literals = type({
singleQuoted: "'typescript'",
doubleQuoted: '"arktype"'
})patterns
A regex literal requires the string to match it. Like a RegExp, it is unanchored unless you anchor it with ^ and $.
A RegExp works as a definition too, e.g. type({ code: /^a.*z$/ }).
Regex literals are typed with ArkRegex, so a pattern that constrains the string's shape narrows the inferred type:
// Type<string>
const const Hex: Type<string>Hex = type("/^[0-9a-fA-F]+$/")
// Type<`${string}.${string}.${string}`>
const const Semver: Type<`${string}.${string}.${string}`>Semver = type("/^(\\d+)\\.(\\d+)\\.(\\d+)$/")e(x)ec mode
Prefix a regex literal with x to transform the string into the result of RegExp.prototype.exec. The output is the match, not the string, and its capture groups are typed:
const const User: Type<{
birthday: (In: `${number}-${number}-${number}`) => Out<RegexExecArray<[`${number}-${number}-${number}`, `${number}`, `${number}`, `${number}`], {
month: `${number}`;
day: `${number}`;
year: `${number}`;
}, "">>;
}>
User = type({
birthday: "x/^(?<month>\\d{2})-(?<day>\\d{2})-(?<year>\\d{4})$/"
})
const const data: {
birthday: (`${number}` | `${number}-${number}-${number}`)[] & {
index: number;
input: `${number}-${number}-${number}`;
indices: undefined;
groups: {
month: `${number}`;
day: `${number}`;
year: `${number}`;
};
0: `${number}-${number}-${number}`;
1: `${number}`;
2: `${number}`;
3: `${number}`;
};
}
data = const User: Type<{
birthday: (In: `${number}-${number}-${number}`) => Out<RegexExecArray<[`${number}-${number}-${number}`, `${number}`, `${number}`, `${number}`], {
month: `${number}`;
day: `${number}`;
year: `${number}`;
}, "">>;
}>
User.assert({ birthday: "05-21-1993" })
const data: {
birthday: (`${number}` | `${number}-${number}-${number}`)[] & {
index: number;
input: `${number}-${number}-${number}`;
indices: undefined;
groups: {
month: `${number}`;
day: `${number}`;
year: `${number}`;
};
0: `${number}-${number}-${number}`;
1: `${number}`;
2: `${number}`;
3: `${number}`;
};
}
data.birthday.groups.month // "05"
const data: {
birthday: (`${number}` | `${number}-${number}-${number}`)[] & {
index: number;
input: `${number}-${number}-${number}`;
indices: undefined;
groups: {
month: `${number}`;
day: `${number}`;
year: `${number}`;
};
0: `${number}-${number}-${number}`;
1: `${number}`;
2: `${number}`;
3: `${number}`;
};
}
data.birthday.groups.year // "1993"lengths
Constrain a string's length with >, >=, <, <=, == or !=:
A range sets both bounds at once, each inclusive or exclusive:
number
number rejects NaN, Infinity and -Infinity. Allow them in one type with the number.NaN, number.Infinity and number.NegativeInfinity keywords:
const const MaybeInfinite: Type<number>MaybeInfinite = type("number | number.Infinity | number.NegativeInfinity")To allow them everywhere, configure numberAllowsNaN or numberAllowsInfinity.
keywords
const const User: Type<{
createdAt: number;
age: number;
}>
User = type({
createdAt: "number.epoch",
age: "number.integer >= 0"
})| keyword | description |
|---|---|
number | a number |
number.Infinity | Infinity |
number.NaN | NaN |
number.NegativeInfinity | -Infinity |
number.epoch | an integer representing a safe Unix timestamp |
number.integer | an integer |
number.safe | at least -9007199254740991 and at most 9007199254740991 |
literals
A number in a definition allows exactly that number:
const const Literals: Type<{
number: 1337;
}>
Literals = type({
number: "1337"
})ranges
Constrain a number with >, >=, < or <=:
A range sets both bounds at once, each inclusive or exclusive:
!= excludes one value:
const const NonZero: Type<number>NonZero = type("number != 0")
// ArkErrors: must be other than 0
NonZero(0)divisors
Constrain a number to a multiple of an integer with %:
bigint
literals
Add the suffix n to a number literal to allow exactly that bigint:
const const Literals: Type<{
bigint: 1337n;
}>
Literals = type({
bigint: "1337n"
})A unit expression like type.unit(1337n) does the same.
symbol
To allow one specific symbol, use a unit expression.
Symbol keys need no special syntax, e.g. { [mySymbol]: "string" }. See properties to make them optional or defaultable.
boolean
literals
null
"null" allows exactly null, usually in a union:
undefined
"undefined" allows exactly undefined, usually in a union:
warningAllowing undefined does not make a key optional
Like in TypeScript, a required key that allows undefined must still be present:
const const MyObj: Type<{
key: number | undefined;
}>
MyObj = type({
key: "number | undefined"
})
// { key: undefined }
MyObj({ key: undefined })
// ArkErrors: key must be a number or undefined (was missing)
MyObj({})