EnumAST node representing a TypeScript enum.
Details
Holds enums as an array of [name, value] pairs where values are
string | number. Parsing succeeds when the input matches any enum value.
export class class Enumclass Enum {
_tag: 'Enum';
enums: ReadonlyArray<readonly [string, string | number]>;
getParser: () => SchemaParser.Parser;
toCodecStringTree: () => AST;
getExpected: () => string;
annotations: Schema.Annotations.Annotations | undefined;
checks: Checks | undefined;
encoding: Encoding | undefined;
context: Context | undefined;
toString: () => string;
}
AST node representing a TypeScript enum.
Details
Holds enums as an array of [name, value] pairs where values are
string | number. Parsing succeeds when the input matches any enum value.
Enum extends class BaseRepresents the abstract base class for all
AST
node variants.
Details
Every AST node extends Base and inherits these fields:
annotations — user-supplied metadata (identifier, title, description,
arbitrary keys).
checks — optional
Checks
for post-type-match validation.
encoding — optional
Encoding
chain for type ↔ wire
transformations.
context — optional
Context
for per-property metadata.
Subclasses add a _tag discriminant and variant-specific data.
Base {
readonly Enum._tag: "Enum"_tag = "Enum"
readonly Enum.enums: readonly (readonly [string, string | number])[]enums: interface ReadonlyArray<T>ReadonlyArray<readonly [string, string | number]>
constructor(
enums: readonly (readonly [
string,
string | number
])[]
enums: interface ReadonlyArray<T>ReadonlyArray<readonly [string, string | number]>,
annotations: anyannotations?: import SchemaSchema.declareAnnotations.type Schema.Annotations.Annotations = /*unresolved*/ anyAnnotations,
checks: Checkschecks?: type Checks = readonly [
Check<any>,
...Check<any>[]
]
Non-empty array of validation
Check
values attached to an AST node
via
Base.checks
.
Details
Checks are run after basic type matching succeeds. They represent
refinements like minLength, pattern, int, etc.
Checks,
encoding: Encodingencoding?: type Encoding = readonly [Link, ...Link[]]A non-empty chain of
Link
values representing the transformation
steps between a schema's decoded (type) form and its encoded (wire) form.
Details
Stored on
Base.encoding
. When undefined, the node has no
encoding transformation (type and encoded forms are identical).
Encoding,
context: Contextcontext?: class Contextclass Context {
isOptional: boolean;
isMutable: boolean;
defaultValue: Encoding | undefined;
annotations: Schema.Annotations.Key<unknown> | undefined;
}
Represents per-property metadata attached to AST nodes via
Base.context
.
Details
Tracks whether a property key is optional, mutable, has a constructor
default, or carries key-level annotations. Typically set by helpers like
optionalKey
and Schema.mutableKey.
isOptional — the property key may be absent from the input.
isMutable — the property is readonly when false.
defaultValue — an
Encoding
applied during construction to
supply missing values.
annotations — key-level annotations (e.g. description of the key
itself).
Context
) {
super(annotations: anyannotations, checks: Checkschecks, encoding: Encodingencoding, context: Contextcontext)
this.Enum.enums: readonly (readonly [string, string | number])[]enums = enums: readonly (readonly [
string,
string | number
])[]
enums
}
/** @internal */
Enum.getParser(): SchemaParser.ParsergetParser() {
const const values: Set<unknown>values = new var Set: SetConstructor
new <unknown>(iterable?: Iterable<unknown> | null | undefined) => Set<unknown> (+1 overload)
Set<unknown>(this.Enum.enums: readonly (readonly [string, string | number])[]enums.ReadonlyArray<readonly [string, string | number]>.map<string | number>(callbackfn: (value: readonly [string, string | number], index: number, array: readonly (readonly [string, string | number])[]) => string | number, thisArg?: any): (string | number)[]Calls a defined callback function on each element of an array, and returns an array that contains the results.
map(([, v: string | numberv]) => v: string | numberv))
return function fromRefinement<T>(
ast: AST,
refinement: (input: unknown) => input is T
): SchemaParser.Parser
fromRefinement(
this,
(input: unknowninput): input: unknowninput is typeof this: thisAST node representing a TypeScript enum.
Details
Holds enums as an array of [name, value] pairs where values are
string | number. Parsing succeeds when the input matches any enum value.
this.Enum.enums: readonly (readonly [string, string | number])[]enums[number][1] => const values: Set<unknown>values.Set<unknown>.has(value: unknown): booleanhas(input: unknowninput)
)
}
/** @internal */
Enum.toCodecStringTree(): ASTtoCodecStringTree(): type AST =
| Declaration
| Null
| Undefined
| Void
| Never
| Unknown
| Any
| String
| Number
| Boolean
| BigInt
| Symbol
| Literal
| UniqueSymbol
| ObjectKeyword
| Enum
| TemplateLiteral
| Arrays
| Objects
| Union<AST>
| Suspend
Discriminated union of all AST node types.
Details
Every Schema has an .ast property of this type. Use the guard functions
(
isString
,
isObjects
, etc.) to narrow to a specific variant,
then access variant-specific fields.
- All variants share the
Base
fields:
annotations, checks,
encoding, context.
- Discriminate on the
_tag field (e.g. "String", "Objects", "Union").
AST {
if (this.Enum.enums: readonly (readonly [string, string | number])[]enums.ReadonlyArray<readonly [string, string | number]>.some(predicate: (value: readonly [string, string | number], index: number, array: readonly (readonly [string, string | number])[]) => unknown, thisArg?: any): booleanDetermines whether the specified callback function returns true for any element of an array.
some(([_: string_, v: string | numberv]) => typeof v: string | numberv === "number")) {
const const coercions: {
[k: string]: string | number
}
coercions = var Object: ObjectConstructorProvides functionality common to all JavaScript objects.
Object.ObjectConstructor.fromEntries<string | number>(entries: Iterable<readonly [PropertyKey, string | number]>): {
[k: string]: string | number;
} (+1 overload)
Returns an object created by key-value entries for properties and methods
fromEntries(this.Enum.enums: readonly (readonly [string, string | number])[]enums.ReadonlyArray<readonly [string, string | number]>.map<[string, string | number]>(callbackfn: (value: readonly [string, string | number], index: number, array: readonly (readonly [string, string | number])[]) => [string, string | number], thisArg?: any): [string, string | number][]Calls a defined callback function on each element of an array, and returns an array that contains the results.
map(([_: string_, v: string | numberv]) => [module globalThisglobalThis.var String: StringConstructor
;(value?: any) => string
Allows manipulation and formatting of text strings and determination and location of substrings within strings.
String(v: string | numberv), v: string | numberv]))
return function replaceEncoding<A extends AST>(
ast: A,
encoding: Encoding | undefined
): A
replaceEncoding(this, [
new constructor Link(to: AST, transformation: SchemaTransformation.Transformation<any, any, any, any> | SchemaTransformation.Middleware<any, any, any, any, any, any>): LinkRepresents a single step in an
Encoding
chain.
Details
A link pairs a target
AST
with a Transformation or Middleware
that converts values between the current node and the target.
to — the AST node on the other side of this transformation step.
transformation — the bidirectional conversion logic (decode/encode).
Links are composed into a non-empty array (
Encoding
) attached to
AST nodes that have a different encoded representation.
Link(
new constructor Union<Literal>(types: readonly Literal[], mode: "anyOf" | "oneOf", annotations?: Schema.Annotations.Annotations, checks?: Checks, encoding?: Encoding, context?: Context, encodingChecks?: Checks): Union<Literal>AST node representing a union of schemas.
Details
types — the member AST nodes.
mode — "anyOf" succeeds on the first match (like TypeScript unions);
"oneOf" requires exactly one member to match (fails if multiple do).
During parsing, members are tried in order. An internal candidate index
narrows which members to try based on the runtime type of the input and
discriminant ("sentinel") fields, making large unions efficient.
Example (Inspecting a union AST)
import { Schema, SchemaAST } from "effect"
const schema = Schema.Union([Schema.String, Schema.Number])
const ast = schema.ast
if (SchemaAST.isUnion(ast)) {
console.log(ast.types.length) // 2
console.log(ast.mode) // "anyOf"
}
Union(var Object: ObjectConstructorProvides functionality common to all JavaScript objects.
Object.ObjectConstructor.keys(o: {}): string[] (+1 overload)Returns the names of the enumerable string properties and methods of an object.
keys(const coercions: {
[k: string]: string | number
}
coercions).Array<string>.map<Literal>(callbackfn: (value: string, index: number, array: string[]) => Literal, thisArg?: any): Literal[]Calls a defined callback function on each element of an array, and returns an array that contains the results.
map((k: stringk) => new constructor Literal(literal: LiteralValue, annotations?: Schema.Annotations.Annotations, checks?: Checks, encoding?: Encoding, context?: Context): LiteralAST node matching an exact primitive value (string, number, boolean, or
bigint).
Details
Parsing succeeds only when the input is strictly equal (===) to the
stored literal. Numeric literals must be finite — Infinity, -Infinity,
and NaN are rejected at construction time.
Example (Creating a literal AST)
import { SchemaAST } from "effect"
const ast = new SchemaAST.Literal("active")
console.log(ast.literal) // "active"
Literal(k: stringk)), "anyOf"),
new import SchemaTransformationSchemaTransformation.constructor Transformation<string | number, string, never, never>(decode: SchemaGetter.Getter<string | number, string, never>, encode: SchemaGetter.Getter<string, string | number, never>): SchemaTransformation.Transformation<string | number, string, never, never>Represents a bidirectional transformation between a decoded type T and an encoded
type E, built from a pair of Getters.
When to use
Use when you need a schema transformation that defines how a schema converts
between two representations.
- You want to compose multiple transformations into a pipeline.
- You want to flip a transformation to swap decode/encode.
Details
This is the primary building block for Schema.decodeTo, Schema.encodeTo,
Schema.decode, Schema.encode, and Schema.link. Each direction is a
SchemaGetter.Getter that handles optionality, failure, and Effect services.
- Immutable —
flip() and compose() return new instances.
flip() swaps the decode and encode getters.
compose(other) chains: this.decode then other.decode for decoding,
other.encode then this.encode for encoding.
Example (Composing two transformations)
import { SchemaTransformation } from "effect"
const trimAndLower = SchemaTransformation.trim().compose(
SchemaTransformation.toLowerCase()
)
// decode: trim then lowercase
// encode: passthrough (both directions)
Transformation(
import SchemaGetterSchemaGetter.function transform<T, E>(
f: (e: E) => T
): Getter<T, E>
Creates a getter that applies a pure function to present values.
When to use
Use when you need a schema getter for a pure, infallible transformation
between types.
- Building encode/decode pairs for
Schema.decodeTo.
Details
- This is the most commonly used constructor.
- Transforms
Some(e) to Some(f(e)) and leaves None unchanged.
- Skips
None inputs — only called when a value is present.
- Never fails.
Example (Transforming strings to numbers)
import { Schema, SchemaGetter } from "effect"
const NumberFromString = Schema.String.pipe(
Schema.decodeTo(Schema.Number, {
decode: SchemaGetter.transform((s) => Number(s)),
encode: SchemaGetter.transform((n) => String(n))
})
)
transform((s: anys) => const coercions: {
[k: string]: string | number
}
coercions[s: anys]),
import SchemaGetterSchemaGetter.function String<E>(): Getter<string, E>Coerces any value to a string using the global String() constructor.
When to use
Use when you need a schema getter to coerce a present encoded value to a
string with String().
Details
The getter is pure, never fails, and delegates to globalThis.String.
Example (Coercing to a string)
import { SchemaGetter } from "effect"
const toString = SchemaGetter.String<number>()
// Getter<string, number>
String()
)
)
])
}
return this
}
/** @internal */
Enum.getExpected(): stringgetExpected(): string {
return this.Enum.enums: readonly (readonly [string, string | number])[]enums.ReadonlyArray<readonly [string, string | number]>.map<string>(callbackfn: (value: readonly [string, string | number], index: number, array: readonly (readonly [string, string | number])[]) => string, thisArg?: any): string[]Calls a defined callback function on each element of an array, and returns an array that contains the results.
map(([_: string_, value: string | numbervalue]) => var JSON: JSONAn intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON.JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
stringify(value: string | numbervalue)).Array<string>.join(separator?: string): stringAdds all the elements of an array into a string, separated by the specified separator string.
join(" | ")
}
}