Type API
| Name | Summary | Notes & Examples |
|---|---|---|
| $ | Scope in which chained methods are parsed | |
| infer | type of output this returns | 🥸 inference-only property that will be |
| inferIn | type of input this allows | 🥸 inference-only property that will be |
| json | internal JSON representation | |
| toJsonSchema | generate a JSON Schema | |
| meta | metadata like custom descriptions and error messages | |
| description | human-readable English description | ✅ works best for primitive values |
| expression | syntax string similar to native TypeScript | ✅ works well for both primitives and structures |
| assert | validate and return transformed data or throw | |
| allows | check input without applying morphs | ✅ good for stuff like filtering that doesn't benefit from detailed errors |
| configure | add metadata to shallow references | ⚠️ does not affect error messages within properties of an object |
| describe | add description to shallow references | ⚠️ does not affect error messages within properties of an object |
| onUndeclaredKey | apply undeclared key behavior | • • • |
| onDeepUndeclaredKey | deeply apply undeclared key behavior | • • • |
| from | alias for assertwith typed input | |
| brand | add a compile-time brand to output | 🥸 inference-only function that does nothing runtime |
| array | an array of this | |
| optional | optional definition | ⚠️ unlike most other methods, this creates a definition rather than a Type (read why) |
| default | defaultable definition | ✅ object defaults can be returned from a function ⚠️ throws if the default value is not allowed ⚠️ unlike most other methods, this creates a definition rather than a Type (read why) |
| filter | apply a predicate function to input | ✅ most useful for morphs with input types that are re-used externally |
| narrow | apply a predicate function to output | ✅ go-to fallback for validation not composable via built-in types and operators ✅ runs after all other validators and morphs, if present |
| pipe | pipe output through arbitrary transformations or other Types | |
| to | parse a definition as an output validator | 🔗 |
| select | query internal node references | filters and returns the Type's internal representation from |
| as | cast the way this is inferred | 🥸 inference-only function that does nothing runtime |
| and | intersect the parsed Type, throwing if the result is unsatisfiable | |
| or | union with the parsed Type | |
| intersect | intersect the parsed Type, returning an introspectable Disjointif the result is unsatisfiable | |
| equals | check if the parsed Type's constraints are identical | ✅ equal types have identical input and output constraints and transforms |
| ifEquals | narrow this based on an equalscheck | |
| extends | check if this is a subtype of the parsed Type | ✅ a subtype must include all constraints from the base type |
| ifExtends | narrow this based on an extendscheck | |
| overlaps | check if a value could satisfy this and the parsed Type | |
| extract | extract branches extending the parsed Type | |
| exclude | exclude branches extending the parsed Type |
The methods below are available on every Type instance. For validation and traversal methods (assert, allows, direct invocation), see the Traversal API. For composition methods (pipe, to, narrow, filter, and, or), see Expressions. For object-specific methods (pick, omit, required, partial, merge, keyof, get, readonly, map, props), see Objects.
from
from is a typed-input variant of assert. It accepts an input matching inferIn and returns inferOut, providing type safety on both sides. Like assert, it throws a TraversalError on invalid input:
in / out
The in and out getters extract the input or output Type from a morphed Type, stripping transformations:
extends
Check if a Type is a subtype of another:
equals
Check if two Types are structurally identical:
overlaps
Check if any value could satisfy both Types:
extract / exclude
Extract or exclude union branches based on a Type:
distribute
Map and optionally reduce over union branches:
toJsonSchema
Each Type instance exposes a toJsonSchema() method that can be used to generate a corresponding JSON Schema.
Options can be passed to change the behavior including how incompatibilities are handled. See the associated config docs for more details.