ArkType

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[]" })
keyworddescription
stringa string
string.alphaonly letters
string.alphanumericonly letters and digits 0-9
string.base58base58-encoded
string.base64base64-encoded
string.base64.urlbase64url-encoded
string.boolean"false" or "true"
string.boolean.parsea morph from "false" or "true" to boolean
string.capitalizea morph from a string to capitalized
string.capitalize.preformattedcapitalized
string.creditCarda credit card number
string.datea parsable date
string.date.epochan integer string representing a safe Unix timestamp
string.date.epoch.parsea morph from an integer string representing a safe Unix timestamp to a Date
string.date.isoan ISO 8601 date or date-time
string.date.iso.datean ISO 8601 date
string.date.iso.parsea morph from an ISO 8601 date or date-time to a Date
string.date.parsea morph from a parsable date to a Date
string.digitsonly digits 0-9
string.emailan email address
string.hexhex characters only
string.integera well-formed integer string
string.integer.parsea morph from a well-formed integer string to an integer and at least -9007199254740991 and at most 9007199254740991
string.ipan IP address
string.ip.v4an IPv4 address
string.ip.v6an IPv6 address
string.jsona JSON string
string.json.parsea JSON string
string.lowera morph from a string to lowercase
string.lower.preformattedlowercase
string.nonblanknon-blank
string.normalizea morph from a string to NFC-normalized unicode
string.normalize.NFCa morph from a string to NFC-normalized unicode
string.normalize.NFC.preformattedNFC-normalized unicode
string.normalize.NFDa morph from a string to NFD-normalized unicode
string.normalize.NFD.preformattedNFD-normalized unicode
string.normalize.NFKCa morph from a string to NFKC-normalized unicode
string.normalize.NFKC.preformattedNFKC-normalized unicode
string.normalize.NFKDa morph from a string to NFKD-normalized unicode
string.normalize.NFKD.preformattedNFKD-normalized unicode
string.numerica well-formed numeric string
string.numeric.parsea morph from a well-formed numeric string and finite to a number
string.regexa regex pattern
string.semvera semantic version (see https://semver.org/)
string.trima morph from a string to trimmed
string.trim.preformattedtrimmed
string.uppera morph from a string to uppercase
string.upper.preformatteduppercase
string.urla URL string
string.url.httpan HTTP or HTTPS URL
string.url.parsea morph from a URL string to a URL instance
string.uuida UUID
string.uuid.v1a UUIDv1
string.uuid.v2a UUIDv2
string.uuid.v3a UUIDv3
string.uuid.v4a UUIDv4
string.uuid.v5a UUIDv5
string.uuid.v6a UUIDv6
string.uuid.v7a UUIDv7
string.uuid.v8a 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" })
keyworddescription
numbera number
number.InfinityInfinity
number.NaNNaN
number.NegativeInfinity-Infinity
number.epochan integer representing a safe Unix timestamp
number.integeran integer
number.safeat 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({})