Schema.Struct<{
readonly _tag: Schema.tag<"Union">
readonly annotations: Schema.optional<
Schema.decodeTo<
Schema.$Record<Schema.String, Schema.Unknown>,
Schema.$Record<
Schema.String,
Schema.Codec<PrimitiveTree, PrimitiveTree, never, never>
>,
never,
never
>
>
readonly types: Schema.$Array<Schema.suspend<$Representation>>
readonly mode: Schema.Literals<readonly ["anyOf", "oneOf"]>
}>Schema for the Union representation node.
export const const $Union: Schema.Struct<{
readonly _tag: Schema.tag<"Union">
readonly annotations: Schema.optional<
Schema.decodeTo<
Schema.$Record<
Schema.String,
Schema.Unknown
>,
Schema.$Record<
Schema.String,
Schema.Codec<
PrimitiveTree,
PrimitiveTree,
never,
never
>
>,
never,
never
>
>
readonly types: Schema.$Array<
Schema.suspend<$Representation>
>
readonly mode: Schema.Literals<
readonly ["anyOf", "oneOf"]
>
}>
const $Union: {
Type: Struct.Type<Fields>;
Encoded: Struct.Encoded<Fields>;
DecodingServices: Struct.DecodingServices<Fields>;
EncodingServices: Struct.EncodingServices<Fields>;
Iso: Struct.Iso<Fields>;
fields: Fields;
mapFields: (f: (fields: { readonly _tag: Schema.tag<'Union'>; readonly annotations: Schema.optional<Schema.decodeTo<Schema.$Record<Schema.String, Schema.Unknown>, Schema.$Record<Schema.String, Schema.Codec<PrimitiveTree, PrimitiveTree, never, never>>…;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Schema.Annotations.Bottom<{ readonly _tag: 'Union'; readonly types: ReadonlyArray<Representation>; readonly mode: 'anyOf' | 'oneOf'; readonly annotations?: { readonly [x: string]: unknown; } | undefined; }, readonly []>) => S…;
annotateKey: (annotations: Schema.Annotations.Key<{ readonly _tag: 'Union'; readonly types: ReadonlyArray<Representation>; readonly mode: 'anyOf' | 'oneOf'; readonly annotations?: { readonly [x: string]: unknown } | undefined }>) => Schema.Struct<{ rea…;
check: (checks_0: SchemaAST.Check<{ readonly _tag: 'Union'; readonly types: ReadonlyArray<Representation>; readonly mode: 'anyOf' | 'oneOf'; readonly annotations?: { readonly [x: string]: unknown } | undefined }>, ...checks: Array<SchemaAST.Check…;
rebuild: (ast: SchemaAST.Objects) => Schema.Struct<{ readonly _tag: Schema.tag<'Union'>; readonly annotations: Schema.optional<Schema.decodeTo<Schema.$Record<Schema.String, Schema.Unknown>, Schema.$Record<Schema.String, Schema.Codec<PrimitiveTree, …;
make: (input: { readonly types: ReadonlyArray<unknown>; readonly mode: 'anyOf' | 'oneOf'; readonly _tag?: 'Union' | undefined; readonly annotations?: { readonly [x: string]: unknown } | undefined }, options?: MakeOptions) => { readonly _tag: 'Un…;
makeOption: (input: { readonly types: ReadonlyArray<unknown>; readonly mode: 'anyOf' | 'oneOf'; readonly _tag?: 'Union' | undefined; readonly annotations?: { readonly [x: string]: unknown } | undefined }, options?: MakeOptions) => Option_.Option<{ rea…;
makeEffect: (input: { readonly types: ReadonlyArray<unknown>; readonly mode: 'anyOf' | 'oneOf'; readonly _tag?: 'Union' | undefined; readonly annotations?: { readonly [x: string]: unknown } | undefined }, options?: MakeOptions) => Effect.Effect<{ read…;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
Schema for the
Union
representation node.
$Union = import SchemaSchema.function Struct<
Fields extends Struct.Fields
>(fields: Fields): Struct<Fields>
Defines a struct schema from a map of field schemas.
Details
Each field value is a schema. Use
optionalKey
or
optional
to
mark fields as optional, and
mutableKey
to mark them as mutable.
The resulting schema's Type is a readonly object type with the fields'
decoded types. The Encoded form mirrors the field schemas' encoded types.
Example (Defining a basic struct)
import { Schema } from "effect"
const Person = Schema.Struct({
name: Schema.String,
age: Schema.Number,
email: Schema.optionalKey(Schema.String)
})
// { readonly name: string; readonly age: number; readonly email?: string }
type Person = typeof Person.Type
const alice = Schema.decodeUnknownSync(Person)({ name: "Alice", age: 30 })
console.log(alice)
// { name: 'Alice', age: 30 }
Struct({
_tag: Schema.tag<"Union">(property) _tag: {
Type: S["Type"];
Encoded: S["Encoded"];
DecodingServices: S["DecodingServices"];
EncodingServices: S["EncodingServices"];
Iso: S["Iso"];
schema: S;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Schema.Annotations.Bottom<'Union', readonly []>) => Schema.withConstructorDefault<Schema.Literal<'Union'>>;
annotateKey: (annotations: Schema.Annotations.Key<'Union'>) => Schema.withConstructorDefault<Schema.Literal<'Union'>>;
check: (checks_0: SchemaAST.Check<'Union'>, ...checks: Array<SchemaAST.Check<'Union'>>) => Schema.withConstructorDefault<Schema.Literal<'Union'>>;
rebuild: (ast: SchemaAST.Literal) => Schema.withConstructorDefault<Schema.Literal<'Union'>>;
make: (input: 'Union', options?: MakeOptions) => 'Union';
makeOption: (input: 'Union', options?: MakeOptions) => Option_.Option<'Union'>;
makeEffect: (input: 'Union', options?: MakeOptions) => Effect.Effect<'Union', SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
_tag: import SchemaSchema.function tag<
Tag extends SchemaAST.LiteralValue
>(literal: Tag): tag<Tag>
Combines a
Literal
schema with
withConstructorDefault
, making it ideal
for discriminator fields in tagged unions. When constructing via make, the
_tag field can be omitted and will be filled automatically.
Example (Defining a discriminated union tag)
import { Schema } from "effect"
const A = Schema.Struct({ _tag: Schema.tag("A"), value: Schema.Number })
// _tag is optional in make, auto-filled to "A"
const a = A.make({ value: 42 })
// a: { _tag: "A", value: 42 }
tag("Union"),
annotations: Schema.optional<
Schema.decodeTo<
Schema.$Record<Schema.String, Schema.Unknown>,
Schema.$Record<
Schema.String,
Schema.Codec<
PrimitiveTree,
PrimitiveTree,
never,
never
>
>,
never,
never
>
>
(property) annotations: {
Rebuild: optional<S>;
Type: S["Type"];
Encoded: S["Encoded"];
DecodingServices: S["DecodingServices"];
EncodingServices: S["EncodingServices"];
Iso: S["Iso"];
schema: S;
ast: Ast;
annotate: (annotations: Schema.Annotations.Bottom<{ readonly [x: string]: unknown; } | undefined, readonly []>) => Schema.optional<Schema.decodeTo<Schema.$Record<Schema.String, Schema.Unknown>, Schema.$Record<Schema.String, Schema.Codec<PrimitiveTre…;
annotateKey: (annotations: Schema.Annotations.Key<{ readonly [x: string]: unknown } | undefined>) => Schema.optional<Schema.decodeTo<Schema.$Record<Schema.String, Schema.Unknown>, Schema.$Record<Schema.String, Schema.Codec<PrimitiveTree, PrimitiveTree,…;
check: (checks_0: SchemaAST.Check<{ readonly [x: string]: unknown } | undefined>, ...checks: Array<SchemaAST.Check<{ readonly [x: string]: unknown } | undefined>>) => Schema.optional<Schema.decodeTo<Schema.$Record<Schema.String, Schema.Unknown>, …;
rebuild: (ast: SchemaAST.Union<SchemaAST.Undefined | SchemaAST.Objects>) => Schema.optional<Schema.decodeTo<Schema.$Record<Schema.String, Schema.Unknown>, Schema.$Record<Schema.String, Schema.Codec<PrimitiveTree, PrimitiveTree, never, never>>, neve…;
make: (input: { readonly [x: string]: unknown } | undefined, options?: MakeOptions) => { readonly [x: string]: unknown } | undefined;
makeOption: (input: { readonly [x: string]: unknown } | undefined, options?: MakeOptions) => Option_.Option<{ readonly [x: string]: unknown } | undefined>;
makeEffect: (input: { readonly [x: string]: unknown } | undefined, options?: MakeOptions) => Effect.Effect<{ readonly [x: string]: unknown } | undefined, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
annotations: import SchemaSchema.const optional: optionalLambda
<Schema.decodeTo<Schema.$Record<Schema.String, Schema.Unknown>, Schema.$Record<Schema.String, Schema.Codec<PrimitiveTree, PrimitiveTree, never, never>>, never, never>>(self: Schema.decodeTo<Schema.$Record<Schema.String, Schema.Unknown>, Schema.$Record<Schema.String, Schema.Codec<PrimitiveTree, PrimitiveTree, never, never>>, never, never>) => Schema.optional<...>
Type-level representation returned by
optional
.
Marks a struct field as optional, allowing the key to be absent or
undefined.
Details
The resulting property may be absent or explicitly set to undefined.
Equivalent to optionalKey(UndefinedOr(S)).
Use
optionalKey
instead if you want exact optional semantics (absent
only, not undefined).
Example (Defining an optional field accepting undefined)
import { Schema } from "effect"
const schema = Schema.Struct({
name: Schema.String,
age: Schema.optional(Schema.Number)
})
// { readonly name: string; readonly age?: number | undefined }
type Person = typeof schema.Type
optional(const $Annotations: Schema.decodeTo<
Schema.$Record<Schema.String, Schema.Unknown>,
Schema.$Record<
Schema.String,
Schema.Codec<
PrimitiveTree,
PrimitiveTree,
never,
never
>
>,
never,
never
>
const $Annotations: {
Type: To["Type"];
Encoded: From["Encoded"];
DecodingServices: To["DecodingServices"] | From["DecodingServices"] | RD;
EncodingServices: To["EncodingServices"] | From["EncodingServices"] | RE;
Iso: To["Iso"];
from: From;
to: To;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Schema.Annotations.Bottom<{ readonly [x: string]: unknown; }, readonly []>) => Schema.decodeTo<Schema.$Record<Schema.String, Schema.Unknown>, Schema.$Record<Schema.String, Schema.Codec<PrimitiveTree, PrimitiveTree, never, nev…;
annotateKey: (annotations: Schema.Annotations.Key<{ readonly [x: string]: unknown }>) => Schema.decodeTo<Schema.$Record<Schema.String, Schema.Unknown>, Schema.$Record<Schema.String, Schema.Codec<PrimitiveTree, PrimitiveTree, never, never>>, never, neve…;
check: (checks_0: SchemaAST.Check<{ readonly [x: string]: unknown }>, ...checks: Array<SchemaAST.Check<{ readonly [x: string]: unknown }>>) => Schema.decodeTo<Schema.$Record<Schema.String, Schema.Unknown>, Schema.$Record<Schema.String, Schema.Cod…;
rebuild: (ast: SchemaAST.Objects) => Schema.decodeTo<Schema.$Record<Schema.String, Schema.Unknown>, Schema.$Record<Schema.String, Schema.Codec<PrimitiveTree, PrimitiveTree, never, never>>, never, never>;
make: (input: { readonly [x: string]: unknown }, options?: MakeOptions) => { readonly [x: string]: unknown };
makeOption: (input: { readonly [x: string]: unknown }, options?: MakeOptions) => Option_.Option<{ readonly [x: string]: unknown }>;
makeEffect: (input: { readonly [x: string]: unknown }, options?: MakeOptions) => Effect.Effect<{ readonly [x: string]: unknown }, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
Schema for serializing public Schema.Annotations.Annotations values. It
filters out internal annotation keys and non-primitive values during
encoding.
When to use
Use to serialize schema annotations in representation schemas while retaining
only primitive-tree metadata.
Details
Decoding is passthrough. Encoding removes internal annotation keys and values
that are not accepted by $PrimitiveTree.
$Annotations),
types: Schema.$Array<
Schema.suspend<$Representation>
>
(property) types: {
Type: ReadonlyArray<S["Type"]>;
Encoded: ReadonlyArray<S["Encoded"]>;
DecodingServices: S["DecodingServices"];
EncodingServices: S["EncodingServices"];
Iso: ReadonlyArray<S["Iso"]>;
value: S;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Schema.Annotations.Bottom<ReadonlyArray<Representation>, readonly []>) => Schema.$Array<Schema.suspend<$Representation>>;
annotateKey: (annotations: Schema.Annotations.Key<ReadonlyArray<Representation>>) => Schema.$Array<Schema.suspend<$Representation>>;
check: (checks_0: SchemaAST.Check<ReadonlyArray<Representation>>, ...checks: Array<SchemaAST.Check<ReadonlyArray<Representation>>>) => Schema.$Array<Schema.suspend<$Representation>>;
rebuild: (ast: SchemaAST.Arrays) => Schema.$Array<Schema.suspend<$Representation>>;
make: (input: ReadonlyArray<unknown>, options?: MakeOptions) => ReadonlyArray<SchemaRepresentation.Representation>;
makeOption: (input: ReadonlyArray<unknown>, options?: MakeOptions) => Option_.Option<ReadonlyArray<SchemaRepresentation.Representation>>;
makeEffect: (input: ReadonlyArray<unknown>, options?: MakeOptions) => Effect.Effect<ReadonlyArray<SchemaRepresentation.Representation>, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
types: import SchemaSchema.Array<Schema.suspend<$Representation>>(self: Schema.suspend<$Representation>): Schema.$Array<Schema.suspend<$Representation>>
export Array
Defines a ReadonlyArray schema for a given element schema.
Example (Defining an array of strings)
import { Schema } from "effect"
const schema = Schema.Array(Schema.String)
const result = Schema.decodeUnknownSync(schema)(["a", "b", "c"])
console.log(result)
// [ 'a', 'b', 'c' ]
Array(const Representation$ref: Schema.suspend<$Representation>const Representation$ref: {
Type: S["Type"];
Encoded: S["Encoded"];
DecodingServices: S["DecodingServices"];
EncodingServices: S["EncodingServices"];
Iso: S["Iso"];
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Schema.Annotations.Bottom<Representation, any>) => Schema.suspend<$Representation>;
annotateKey: (annotations: Schema.Annotations.Key<Representation>) => Schema.suspend<$Representation>;
check: (checks_0: SchemaAST.Check<Representation>, ...checks: Array<SchemaAST.Check<Representation>>) => Schema.suspend<$Representation>;
rebuild: (ast: SchemaAST.Suspend) => Schema.suspend<$Representation>;
make: (input: unknown, options?: MakeOptions) => SchemaRepresentation.Representation;
makeOption: (input: unknown, options?: MakeOptions) => Option_.Option<SchemaRepresentation.Representation>;
makeEffect: (input: unknown, options?: MakeOptions) => Effect.Effect<SchemaRepresentation.Representation, SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
Representation$ref),
mode: Schema.Literals<
readonly ["anyOf", "oneOf"]
>
(property) mode: {
literals: L;
members: { readonly [K in keyof L]: Literal<L[K]>; };
mapMembers: (f: (members: readonly [Schema.Literal<'anyOf'>, Schema.Literal<'oneOf'>]) => To) => Schema.Union<{ [K in keyof Readonly<To>]: Readonly<To>[K]; }>;
pick: (literals: L2) => Schema.Literals<L2>;
transform: (to: L2) => Schema.Union<readonly [Schema.decodeTo<Schema.Literal<L2['0']>, Schema.Literal<'anyOf'>, never, never>, Schema.decodeTo<Schema.Literal<L2['1']>, Schema.Literal<'oneOf'>, never, never>]>;
Rebuild: Rebuild;
Iso: Iso;
ast: Ast;
Type: T;
Encoded: E;
DecodingServices: RD;
EncodingServices: RE;
annotate: (annotations: Schema.Annotations.Bottom<'anyOf' | 'oneOf', readonly []>) => Schema.Literals<readonly ['anyOf', 'oneOf']>;
annotateKey: (annotations: Schema.Annotations.Key<'anyOf' | 'oneOf'>) => Schema.Literals<readonly ['anyOf', 'oneOf']>;
check: (checks_0: SchemaAST.Check<'anyOf' | 'oneOf'>, ...checks: Array<SchemaAST.Check<'anyOf' | 'oneOf'>>) => Schema.Literals<readonly ['anyOf', 'oneOf']>;
rebuild: (ast: SchemaAST.Union<SchemaAST.Literal>) => Schema.Literals<readonly ['anyOf', 'oneOf']>;
make: (input: 'anyOf' | 'oneOf', options?: MakeOptions) => 'anyOf' | 'oneOf';
makeOption: (input: 'anyOf' | 'oneOf', options?: MakeOptions) => Option_.Option<'anyOf' | 'oneOf'>;
makeEffect: (input: 'anyOf' | 'oneOf', options?: MakeOptions) => Effect.Effect<'anyOf' | 'oneOf', SchemaError, never>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
mode: import SchemaSchema.function Literals<
L extends ReadonlyArray<SchemaAST.LiteralValue>
>(literals: L): Literals<L>
Creates a union schema from an array of literal values.
Example (Defining status codes)
import { Schema } from "effect"
const schema = Schema.Literals(["active", "inactive", "pending"])
// accepts "active", "inactive", or "pending"
Literals(["anyOf", "oneOf"])
}).Bottom<unknown, unknown, unknown, unknown, Objects, Struct<{ readonly _tag: tag<"Union">; readonly annotations: optional<decodeTo<$Record<String, Unknown>, $Record<String, Codec<PrimitiveTree, PrimitiveTree, never, never>>, never, never>>; readonly types: $Array<...>; readonly mode: Literals<...>; }>, ... 8 more ..., "required">.annotate(annotations: Schema.Annotations.Bottom<{
readonly _tag: "Union";
readonly types: readonly Representation[];
readonly mode: "anyOf" | "oneOf";
readonly annotations?: {
readonly [x: string]: unknown;
} | undefined;
}, readonly []>): Schema.Struct<{
readonly _tag: Schema.tag<"Union">;
readonly annotations: Schema.optional<Schema.decodeTo<Schema.$Record<Schema.String, Schema.Unknown>, Schema.$Record<Schema.String, Schema.Codec<PrimitiveTree, PrimitiveTree, never, never>>, never, never>>;
readonly types: Schema.$Array<...>;
readonly mode: Schema.Literals<...>;
}>
annotate({ Annotations.Bottom<T, TypeParameters extends ReadonlyArray<Constraint>>.identifier?: string | undefinedStable identifier for this schema node.
Details
Identifiers are used by schema tooling, including JSON Schema
generation, to name references. The default formatter also uses
identifier as the expected label for type-level failures, such as
Expected UserId, got null.
identifier does not name a failed filter or refinement. If the base
type matches and a filter fails, put expected or message on the
filter/refinement instead.
identifier: "Union" })