Skip to content

PatternBuilder

Defined in: src/schema/index.ts:257

The pattern builder. Each element method appends one element and returns a builder whose LAST element can be named with .as(); .result() (trailing) declares the node’s result type, after which .eval() attaches an evaluation function checked against it.

The "~"-prefixed properties carry the accumulated state for defineNode’s inference — they are internal.

TPattern extends readonly PatternSchema[] = readonly []

$

$ extends BindingScope = { }

readonly ~pattern: TPattern

Defined in: src/schema/index.ts:261


readonly ~resultType: undefined

Defined in: src/schema/index.ts:262


readonly ~eval: undefined

Defined in: src/schema/index.ts:263

number(): NameableBuilder<TPattern, $, NumberSchema>

Defined in: src/schema/index.ts:266

Append a number literal element

NameableBuilder<TPattern, $, NumberSchema>


string<TQuotes>(quotes): NameableBuilder<TPattern, $, StringSchema<TQuotes>>

Defined in: src/schema/index.ts:270

Append a string literal element, e.g. p.string(['"', "'"]) — escapes are processed

TQuotes extends readonly string[]

TQuotes

NameableBuilder<TPattern, $, StringSchema<TQuotes>>


ident(): NameableBuilder<TPattern, $, IdentSchema>

Defined in: src/schema/index.ts:275

Append an identifier element (single segment, no dots)

NameableBuilder<TPattern, $, IdentSchema>


path(): NameableBuilder<TPattern, $, PathSchema>

Defined in: src/schema/index.ts:280

Append a member-access path element: matches ident(.ident)* (e.g. values.password), type resolved by walking the schema. Whitespace around the dots is not allowed.

NameableBuilder<TPattern, $, PathSchema>


constVal<TValues>(…values): NameableBuilder<TPattern, $, ConstSchema<TValues>>

Defined in: src/schema/index.ts:301

Append a constant element — one or more values as an ORDERED alternation (first match wins). IDENTIFIER-LIKE values (constVal("null"), constVal("and")) match only as a WHOLE identifier — nullable or andy never match (word-boundary rule; pinned in design-claims); other values ("+", "==") match as raw text, per member. Keyword literals are ordinary const-pattern nodes; name the element to receive the MATCHED text in eval:

TValues extends readonly [string, string]

TValues

NameableBuilder<TPattern, $, ConstSchema<TValues>>

const booleanLit = defineNode({
name: "bool",
precedence: 5,
pattern: (p) =>
p.constVal("true", "false").as("word")
.result("boolean")
.eval(({ word }) => word() === "true"),
});

operand(): NameableBuilder<TPattern, $, ExprSchema<undefined, "operand">>

Defined in: src/schema/index.ts:316

Append a TIGHTER-LEVEL expression element.

Parses at the next-higher grammar level, avoiding left-recursion. A pattern whose final operand is operand() makes its level LEFT-associative (the engine folds repetitions: a-b-c(a-b)-c).

Constraint forms: operand(“number”), operand(“string | number”), operand(“left”) (a binding reference — not valid at position 0, where no earlier operand exists), operand().

NameableBuilder<TPattern, $, ExprSchema<undefined, "operand">>

operand<C>(constraint): NameableBuilder<TPattern, $, ExprSchema<C, "operand">>

Defined in: src/schema/index.ts:317

C extends string

[C] extends [" anyOrNever"] ? C : C extends OptionalPropertyDefinition ? "Optional definitions like 'string?' are only valid as properties in an object or tuple​" : isDefaultable<C, $, bindThis<C>> extends true ? "Defaultable definitions like 'number = 0' are only valid as properties in an object or tuple​" : validateInnerDefinition<C, $, bindThis<C>>

NameableBuilder<TPattern, $, ExprSchema<C, "operand">>

operand<B>(constraint): NameableBuilder<TPattern, $, ExprSchema<OverlapsRef<B>, "operand">>

Defined in: src/schema/index.ts:320

B extends string

OverlapsRef<ValidateOverlapsTarget<B, $>>

NameableBuilder<TPattern, $, ExprSchema<OverlapsRef<B>, "operand">>


rest(): NameableBuilder<TPattern, $, ExprSchema<undefined, "rest">>

Defined in: src/schema/index.ts:333

Append a CURRENT-LEVEL expression element.

Parses at the same level. A pattern whose final operand is rest() makes its level RIGHT-associative (a ^ b ^ ca ^ (b ^ c)).

Constraint forms: rest(“number”), rest(“left”), rest(“left | null”), rest(overlapping(“left”)), rest().

NameableBuilder<TPattern, $, ExprSchema<undefined, "rest">>

rest<C>(constraint): NameableBuilder<TPattern, $, ExprSchema<C, "rest">>

Defined in: src/schema/index.ts:334

C extends string

[C] extends [" anyOrNever"] ? C : C extends OptionalPropertyDefinition ? "Optional definitions like 'string?' are only valid as properties in an object or tuple​" : isDefaultable<C, $, bindThis<C>> extends true ? "Defaultable definitions like 'number = 0' are only valid as properties in an object or tuple​" : validateInnerDefinition<C, $, bindThis<C>>

NameableBuilder<TPattern, $, ExprSchema<C, "rest">>

rest<B>(constraint): NameableBuilder<TPattern, $, ExprSchema<OverlapsRef<B>, "rest">>

Defined in: src/schema/index.ts:337

B extends string

OverlapsRef<ValidateOverlapsTarget<B, $>>

NameableBuilder<TPattern, $, ExprSchema<OverlapsRef<B>, "rest">>


expr(): NameableBuilder<TPattern, $, ExprSchema<undefined, "expr">>

Defined in: src/schema/index.ts:349

Append a FULL expression element.

Resets to the full grammar (precedence 0). Only for DELIMITED contexts — every expr() must be followed by at least one constVal in the same pattern (parentheses’ “)”, ternary’s “:”), otherwise it would swallow looser operators and break precedence.

NameableBuilder<TPattern, $, ExprSchema<undefined, "expr">>

expr<C>(constraint): NameableBuilder<TPattern, $, ExprSchema<C, "expr">>

Defined in: src/schema/index.ts:350

C extends string

[C] extends [" anyOrNever"] ? C : C extends OptionalPropertyDefinition ? "Optional definitions like 'string?' are only valid as properties in an object or tuple​" : isDefaultable<C, $, bindThis<C>> extends true ? "Defaultable definitions like 'number = 0' are only valid as properties in an object or tuple​" : validateInnerDefinition<C, $, bindThis<C>>

NameableBuilder<TPattern, $, ExprSchema<C, "expr">>

expr<B>(constraint): NameableBuilder<TPattern, $, ExprSchema<OverlapsRef<B>, "expr">>

Defined in: src/schema/index.ts:353

B extends string

OverlapsRef<ValidateOverlapsTarget<B, $>>

NameableBuilder<TPattern, $, ExprSchema<OverlapsRef<B>, "expr">>


result<R>(def): ResultedBuilder<TPattern, R>

Defined in: src/schema/index.ts:369

Declare the node’s result type (trailing — after the last element):

  • a binding name (“then”) — derived per-parse from that operand
  • an arktype def (“boolean”, { min: “number”, max: “number” }) — the node mints a type; defs may EMBED binding references (“then | null”), resolved per-parse

Validated by arktype with the chain’s bindings in scope. Required for every node that CONSTRUCTS a result; only a single-element passthrough pattern omits it. (The “~resolved” key is reserved — enforced at construction.)

R extends ResultSpec

[R] extends [" anyOrNever"] ? R : R extends OptionalPropertyDefinition ? "Optional definitions like 'string?' are only valid as properties in an object or tuple​" : isDefaultable<R, $, bindThis<R>> extends true ? "Defaultable definitions like 'number = 0' are only valid as properties in an object or tuple​" : validateInnerDefinition<R, $, bindThis<R>>

ResultedBuilder<TPattern, R>