Adding constraints
Narrow types beyond what TypeScript can express, with keywords, ranges, divisors and custom predicates.
TypeScript can represent types like string or number, but what about email or integer less than 100?
In ArkType, conditions that narrow a type beyond its basis are called constraints. They compose with TypeScript's operators like any other part of a definition.
Define
Let's create a new Contact Type that enforces our example constraints.
const const Contact: Type<{
email: string;
score: number;
}>
Contact = type({
// many common constraints are available as built-in keywords
email: "string.email",
// others can be written as type-safe expressions
score: "number.integer < 100"
})
// infer the TypeScript type as usual
type type Contact = {
email: string;
score: number;
}
Contact = typeof const Contact: Type<{
email: string;
score: number;
}>
Contact.inferinfonumber excludes NaN and Infinity
number rejects NaN, Infinity and -Infinity by default. To allow them,
see number.NaN, number.Infinity and number.NegativeInfinity in
Keywords, or the numberAllowsNaN and
numberAllowsInfinity options in Configuration.
Compose
Imagine we want to define a new Type representing a non-empty list of Contact.
Definitions create new types. Chained methods refine or transform types you already have.
// a non-empty list of Contact
const const Contacts: Type<{
email: string;
score: number;
}[]>
Contacts = const Contact: Type<{
email: string;
score: number;
}>
Contact.array().atLeastLength(1)
// ArkErrors: must be non-empty
const const out: ArkErrors | {
email: string;
score: number;
}[]
out = Contacts([])ArkType's built-in error messages are descriptive and compose across unions automatically (for example, "string.email | string.uuid" reports must be an email address or a UUID), so try them before customizing everything. When you do want custom sentences per check, pass a message as the last argument to any chained constraint method:
const const Username: Type<string>Username = type.string
.atLeastLength(3, "Use 3 or more characters")
.matching(/^[a-z0-9_]*$/, "Use lowercase letters, numbers or underscores")
// ArkErrors: Use 3 or more characters
Username("ab")
// ArkErrors: Use lowercase letters, numbers or underscores
Username("Bad Name")See Error messages for built-in messages, composable .describe() wording, and whole-Type .message().
Narrow
Structured constraints like divisors and ranges only go so far. For a condition they can't express, add your own logic with .narrow. A custom message on a length, range or pattern still belongs on its constraint method, as above.
// there's no "not divisible" expression, so narrow
const const Odd: Type<number>Odd = type("number.integer").narrow(
// ctx.mustBe reports "must be odd" and returns false
(n, ctx) => n % 2 !== 0 || ctx.mustBe("odd")
)
const const FavoriteNumbers: Type<{
even: number;
odd: number;
}>
FavoriteNumbers = type({
even: "number % 2",
odd: const Odd: Type<number>Odd
})
// ArkErrors: even must be even
// odd must be odd
FavoriteNumbers({ even: 7, odd: 8 })You now know how to refine your types to enforce additional constraints at runtime.
But what if once your input is fully validated, you still need to make some adjustments before it's ready to use?
The final section of the intro covers morphs, which transform data as part of a Type.