Skip to content

Parser

Defined in: src/createParser.ts:175

Parser interface with type-safe parse methods.

Schemas are arktype object defs, validated at compile time by type.validate IN THE PARSER’S SCOPE on EVERY entry point — a typo’d leaf like { x: “numbr” } errors at the leaf, while an alias leaf like { created: “Timestamp” } resolves when the parser was created with { scope: { Timestamp: … } }. The same scope drives literal-mode parsing (identifier/path types) and runtime compilation.

History note: parse/evaluate could not carry type.validate before the 2026-07 rework — combined with their deferred-conditional input parameter, inference was METASTABLE (declaration-order-dependent). The rework’s depth reductions moved the shape off the instantiation edge; re-tested 2026-07-07 across 8 declaration-order permutations × TS 5.7/5.9/6.0, warm and cold (pinned in design-claims.typetest.ts). evaluate’s values stay NoInfer-wrapped — WITHOUT it, TS inverts the mapped type inside type.infer and silently unions the values argument into TSchema (also pinned there).

TGrammar extends Grammar

TNodes extends readonly NodeSchema[]

$

$ extends object = { }

readonly nodes: TNodes

Defined in: src/createParser.ts:255

The node schemas used to create this parser

parse<TInput, TSchema>(input, schema): Parse<TGrammar, TInput, Context<TSchema, $>>

Defined in: src/createParser.ts:188

Parse a string literal, validated at compile time.

The input must be a literal that FULLY parses against the grammar — anything else is a compile-time error. For runtime-provided strings, use safeParse. Throws StringentParseError if the compile-time check was bypassed and the input is invalid.

TInput extends string

TSchema extends SchemaShape

ValidatedInput<TGrammar, TInput, Context<TSchema, $>>

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

Parse<TGrammar, TInput, Context<TSchema, $>>


safeParse<TInput, TSchema>(input, schema): SafeParseResult<AstFor<TGrammar, TInput, TSchema, $>>

Defined in: src/createParser.ts:202

Parse any string (including runtime-provided input).

Requires the whole input to be consumed. NEVER throws: invalid input returns a structured error with position/expected/found; an invalid schema (a programmer error, normally caught at compile time by type.validate) returns { success: false, error: { code: “INVALID_SCHEMA” } }.

TInput extends string

TSchema extends SchemaShape

TInput

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

SafeParseResult<AstFor<TGrammar, TInput, TSchema, $>>


evaluate<TInput, TSchema>(input, schema, values): EvaluateResult<TGrammar, TInput, TSchema, $>

Defined in: src/createParser.ts:213

Parse a string literal and evaluate it against runtime values.

Node eval() functions (from defineNode) compute the result. The values object is validated against the schema before evaluation.

TInput extends string

TSchema extends SchemaShape

ValidatedInput<TGrammar, TInput, Context<TSchema, $>>

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

NoInfer<inferDefinition<TSchema, $, bindThis<TSchema>>>

EvaluateResult<TGrammar, TInput, TSchema, $>


evaluateAst<TAst>(ast, values): InferOfDef<TAst["outputSchema"]>

Defined in: src/createParser.ts:226

Evaluate an already-parsed AST against runtime values. Use with safeParse for dynamic input.

TAst extends object

TAst

EvaluationValues

InferOfDef<TAst["outputSchema"]>


compile<TInput, TSchema>(input, schema, options?): CompiledRule<TGrammar, TInput, TSchema, $>

Defined in: src/createParser.ts:248

Compile a rule into an arktype Type (D12) — the ecosystem bridge.

A boolean-output rule becomes a PREDICATE Type: it validates the values object against the schema (refinements included), evaluates the rule, and rejects with an ArkErrors entry at options.path when the rule is false. Any other rule becomes a MORPH Type: values in, evaluated result out. Either way the result is a real arktype Type — a Standard Schema — so it plugs into react-hook-form, tRPC, hono, … .in is the values contract; predicate rules carry a predicate node, so JSON Schema export needs the fallback: rule.in.toJsonSchema({ fallback: { predicate: (ctx) => ctx.base } }).

Unlike parse/evaluate, dynamic strings ARE accepted (rules often live in config); invalid input throws StringentParseError at compile time — literal inputs additionally get precise compile-time typing.

TInput extends string

TSchema extends SchemaShape

TInput

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

CompileRuleOptions

CompiledRule<TGrammar, TInput, TSchema, $>