TaggedUnion<Cases>Builds a discriminated union from a record of field sets, one per variant.
Each key becomes the _tag literal and the value is passed to TaggedStruct.
The result includes cases, guards, isAnyOf, and match utilities.
Example (Pattern matching a discriminated union)
import { Schema } from "effect"
const Shape = Schema.TaggedUnion({
Circle: { radius: Schema.Number },
Rectangle: { width: Schema.Number, height: Schema.Number }
})
// Pattern-match on a decoded value
const area = Shape.match({ _tag: "Circle", radius: 5 }, {
Circle: (c) => Math.PI * c.radius ** 2,
Rectangle: (r) => r.width * r.height
})export interface interface TaggedUnion<Cases extends Record<string, Constraint>>Builds a discriminated union from a record of field sets, one per variant.
Each key becomes the _tag literal and the value is passed to
TaggedStruct
.
The result includes cases, guards, isAnyOf, and match utilities.
Example (Pattern matching a discriminated union)
import { Schema } from "effect"
const Shape = Schema.TaggedUnion({
Circle: { radius: Schema.Number },
Rectangle: { width: Schema.Number, height: Schema.Number }
})
// Pattern-match on a decoded value
const area = Shape.match({ _tag: "Circle", radius: 5 }, {
Circle: (c) => Math.PI * c.radius ** 2,
Rectangle: (r) => r.width * r.height
})
Type-level representation returned by
TaggedUnion
.
TaggedUnion<function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases extends type Record<K extends keyof any, T> = {
[P in K]: T
}
Construct a type with a set of properties K of type T
Record<string, Constraint>> extends
interface BottomLazy<out Ast extends SchemaAST.AST, out Rebuild extends Top, in out TypeParameters extends ReadonlyArray<Constraint> = readonly [], out TypeMutability extends Mutability = "readonly", out TypeOptionality extends Optionality = "required", out TypeConstructorDefault extends ConstructorDefault = "no-default", out EncodedMutability extends Mutability = "readonly", out EncodedOptionality extends Optionality = "required">Lazy Bottom variant for schema implementations that compute their public
views on demand.
When to use
Use as an implementation base for schema interfaces that must expose
Bottom behavior without forcing TypeScript to eagerly evaluate expensive
Type, Encoded, or service views.
Details
The laziness is purely type-level; runtime behavior is unchanged.
BottomLazy keeps the structural operations inherited from Bottom, but
erases the expensive schema views to unknown. Concrete schema interfaces can
then redeclare the precise views they expose. This keeps wide schemas such as
Struct and Union cheaper when generic code reads a single view, while
preserving their exact public types.
BottomLazy<
import SchemaASTSchemaAST.class Union<A extends SchemaAST.AST = SchemaAST.AST>class Union {
_tag: 'Union';
types: ReadonlyArray<A>;
mode: "anyOf" | "oneOf";
encodingChecks: Checks | undefined;
getParser: (recur: (ast: AST) => SchemaParser.Parser) => SchemaParser.Parser;
_rebuild: (recur: (ast: AST) => AST, checks: Checks | undefined, encodingChecks: Checks | undefined) => Union<AST>;
recur: (recur: (ast: AST) => AST) => Union<AST>;
flip: (recur: (ast: AST) => AST) => Union<AST>;
matchPart: (s: string, options: ParseOptions) => LiteralValue | undefined;
getExpected: (getExpected: (ast: AST) => string) => string;
annotations: Schema.Annotations.Annotations | undefined;
checks: Checks | undefined;
encoding: Encoding | undefined;
context: Context | undefined;
toString: () => string;
}
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<import SchemaASTSchemaAST.class Objectsclass Objects {
_tag: 'Objects';
propertySignatures: ReadonlyArray<PropertySignature>;
indexSignatures: ReadonlyArray<IndexSignature>;
encodingChecks: Checks | undefined;
getParser: (recur: (ast: AST) => SchemaParser.Parser) => SchemaParser.Parser;
_rebuild: (recur: (ast: AST) => AST, recurParameter: (ast: AST) => AST, flipMerge: boolean, checks: Checks | undefined, encodingChecks: Checks | undefined) => Objects;
flip: (recur: (ast: AST) => AST) => AST;
recur: (recur: (ast: AST) => AST, recurParameter?: (ast: AST) => AST) => AST;
getExpected: () => string;
annotations: Schema.Annotations.Annotations | undefined;
checks: Checks | undefined;
encoding: Encoding | undefined;
context: Context | undefined;
toString: () => string;
}
AST node for object-like schemas, including structs and records.
When to use
Use when constructing or inspecting AST nodes for structs or records rather
than array-like schemas.
Details
propertySignatures — named properties with their types (struct fields).
indexSignatures — index signature entries (record patterns), each with
a parameter AST for matching keys and a type AST for values.
An Objects node with no properties and no index signatures performs only a
non-nullish check: it accepts any value except null and undefined,
including primitive values.
Gotchas
Duplicate property names throw at construction time.
Example (Inspecting a struct AST)
import { Schema, SchemaAST } from "effect"
const schema = Schema.Struct({ name: Schema.String })
const ast = schema.ast
if (SchemaAST.isObjects(ast)) {
for (const ps of ast.propertySignatures) {
console.log(ps.name, ps.type._tag)
}
// "name" "String"
}
Objects>,
interface TaggedUnion<Cases extends Record<string, Constraint>>Builds a discriminated union from a record of field sets, one per variant.
Each key becomes the _tag literal and the value is passed to
TaggedStruct
.
The result includes cases, guards, isAnyOf, and match utilities.
Example (Pattern matching a discriminated union)
import { Schema } from "effect"
const Shape = Schema.TaggedUnion({
Circle: { radius: Schema.Number },
Rectangle: { width: Schema.Number, height: Schema.Number }
})
// Pattern-match on a decoded value
const area = Shape.match({ _tag: "Circle", radius: 5 }, {
Circle: (c) => Math.PI * c.radius ** 2,
Rectangle: (r) => r.width * r.height
})
Type-level representation returned by
TaggedUnion
.
TaggedUnion<function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases>
>
{
readonly "Type": { [function (type parameter) KK in keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]: function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases[function (type parameter) KK]["Type"] }[keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]
readonly "Encoded": { [function (type parameter) KK in keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]: function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases[function (type parameter) KK]["Encoded"] }[keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]
readonly "DecodingServices": { [function (type parameter) KK in keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]: function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases[function (type parameter) KK]["DecodingServices"] }[keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]
readonly "EncodingServices": { [function (type parameter) KK in keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]: function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases[function (type parameter) KK]["EncodingServices"] }[keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]
readonly "~type.make.in": { [function (type parameter) KK in keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]: function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases[function (type parameter) KK]["~type.make"] }[keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]
readonly "~type.make": { [function (type parameter) KK in keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]: function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases[function (type parameter) KK]["~type.make"] }[keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]
readonly "Iso": { [function (type parameter) KK in keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]: function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases[function (type parameter) KK]["Type"] }[keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]
readonly TaggedUnion<Cases extends Record<string, Constraint>>.cases: Cases extends Record<string, Constraint>cases: function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases
readonly TaggedUnion<Cases extends Record<string, Constraint>>.isAnyOf: <const Keys>(keys: ReadonlyArray<Keys>) => (value: Cases[keyof Cases]["Type"]) => value is Extract<Cases[keyof Cases]["Type"], {
_tag: Keys;
}>
isAnyOf: <const function (type parameter) Keys in <const Keys>(keys: ReadonlyArray<Keys>): (value: Cases[keyof Cases]["Type"]) => value is Extract<Cases[keyof Cases]["Type"], {
_tag: Keys;
}>
Keys>(
keys: readonly Keys[]keys: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) Keys in <const Keys>(keys: ReadonlyArray<Keys>): (value: Cases[keyof Cases]["Type"]) => value is Extract<Cases[keyof Cases]["Type"], {
_tag: Keys;
}>
Keys>
) => (value: Cases[keyof Cases]["Type"]value: function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases[keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]["Type"]) => value: Cases[keyof Cases]["Type"]value is type Extract<T, U> = T extends U
? T
: never
Extract from T those types that are assignable to U
Extract<function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases[keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]["Type"], { _tag: const Keys_tag: function (type parameter) Keys in <const Keys>(keys: ReadonlyArray<Keys>): (value: Cases[keyof Cases]["Type"]) => value is Extract<Cases[keyof Cases]["Type"], {
_tag: Keys;
}>
Keys }>
readonly TaggedUnion<Cases extends Record<string, Constraint>>.guards: { [K in keyof Cases]: (u: unknown) => u is Cases[K]["Type"]; }guards: { [function (type parameter) KK in keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]: (u: unknownu: unknown) => u: unknownu is function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases[function (type parameter) KK]["Type"] }
readonly TaggedUnion<Cases extends Record<string, Constraint>>.match: {
<Output>(cases: { [K in keyof Cases]: (value: Cases[K]["Type"]) => Output; }): (value: Cases[keyof Cases]["Type"]) => Output;
<Output>(value: Cases[keyof Cases]["Type"], cases: { [K in keyof Cases]: (value: Cases[K]["Type"]) => Output; }): Output;
}
match: {
<function (type parameter) Output in <Output>(cases: { [K in keyof Cases]: (value: Cases[K]["Type"]) => Output; }): (value: Cases[keyof Cases]["Type"]) => OutputOutput>(
cases: {
[K in keyof Cases]: (
value: Cases[K]["Type"]
) => Output
}
cases: { [function (type parameter) KK in keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]: (value: Cases[K]["Type"]value: function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases[function (type parameter) KK]["Type"]) => function (type parameter) Output in <Output>(cases: { [K in keyof Cases]: (value: Cases[K]["Type"]) => Output; }): (value: Cases[keyof Cases]["Type"]) => OutputOutput }
): (value: Cases[keyof Cases]["Type"]value: function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases[keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]["Type"]) => function (type parameter) Output in <Output>(cases: { [K in keyof Cases]: (value: Cases[K]["Type"]) => Output; }): (value: Cases[keyof Cases]["Type"]) => OutputOutput
<function (type parameter) Output in <Output>(value: Cases[keyof Cases]["Type"], cases: { [K in keyof Cases]: (value: Cases[K]["Type"]) => Output; }): OutputOutput>(
value: Cases[keyof Cases]["Type"]value: function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases[keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]["Type"],
cases: {
[K in keyof Cases]: (
value: Cases[K]["Type"]
) => Output
}
cases: { [function (type parameter) KK in keyof function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases]: (value: Cases[K]["Type"]value: function (type parameter) Cases in TaggedUnion<Cases extends Record<string, Constraint>>Cases[function (type parameter) KK]["Type"]) => function (type parameter) Output in <Output>(value: Cases[keyof Cases]["Type"], cases: { [K in keyof Cases]: (value: Cases[K]["Type"]) => Output; }): OutputOutput }
): function (type parameter) Output in <Output>(value: Cases[keyof Cases]["Type"], cases: { [K in keyof Cases]: (value: Cases[K]["Type"]) => Output; }): OutputOutput
}
}
/**
* Builds a discriminated union from a record of field sets, one per variant.
* Each key becomes the `_tag` literal and the value is passed to {@link TaggedStruct}.
* The result includes `cases`, `guards`, `isAnyOf`, and `match` utilities.
*
* **Example** (Pattern matching a discriminated union)
*
* ```ts
* import { Schema } from "effect"
*
* const Shape = Schema.TaggedUnion({
* Circle: { radius: Schema.Number },
* Rectangle: { width: Schema.Number, height: Schema.Number }
* })
*
* // Pattern-match on a decoded value
* const area = Shape.match({ _tag: "Circle", radius: 5 }, {
* Circle: (c) => Math.PI * c.radius ** 2,
* Rectangle: (r) => r.width * r.height
* })
* ```
*
* @see {@link toTaggedUnion} to augment an existing union instead
* @category constructors
* @since 4.0.0
*/
export function function TaggedUnion<
CasesByTag extends Record<string, Struct.Fields>
>(
casesByTag: CasesByTag
): TaggedUnion<{
readonly [K in keyof CasesByTag &
string]: TaggedStruct<K, CasesByTag[K]>
}>
Builds a discriminated union from a record of field sets, one per variant.
Each key becomes the _tag literal and the value is passed to
TaggedStruct
.
The result includes cases, guards, isAnyOf, and match utilities.
Example (Pattern matching a discriminated union)
import { Schema } from "effect"
const Shape = Schema.TaggedUnion({
Circle: { radius: Schema.Number },
Rectangle: { width: Schema.Number, height: Schema.Number }
})
// Pattern-match on a decoded value
const area = Shape.match({ _tag: "Circle", radius: 5 }, {
Circle: (c) => Math.PI * c.radius ** 2,
Rectangle: (r) => r.width * r.height
})
TaggedUnion<const function (type parameter) CasesByTag in TaggedUnion<const CasesByTag extends Record<string, Struct.Fields>>(casesByTag: CasesByTag): TaggedUnion<{ readonly [K in keyof CasesByTag & string]: TaggedStruct<K, CasesByTag[K]>; }>CasesByTag extends type Record<K extends keyof any, T> = {
[P in K]: T
}
Construct a type with a set of properties K of type T
Record<string, Struct.type Struct<Fields extends Struct.Fields>.Fields = {
readonly [x: string]: Constraint;
readonly [x: number]: Constraint;
readonly [x: symbol]: Constraint;
}
Constraint for a struct field map: an object whose values are schemas.
Fields>>(
casesByTag: const CasesByTag extends Record<string, Struct.Fields>casesByTag: function (type parameter) CasesByTag in TaggedUnion<const CasesByTag extends Record<string, Struct.Fields>>(casesByTag: CasesByTag): TaggedUnion<{ readonly [K in keyof CasesByTag & string]: TaggedStruct<K, CasesByTag[K]>; }>CasesByTag
): interface TaggedUnion<Cases extends Record<string, Constraint>>Builds a discriminated union from a record of field sets, one per variant.
Each key becomes the _tag literal and the value is passed to
TaggedStruct
.
The result includes cases, guards, isAnyOf, and match utilities.
Example (Pattern matching a discriminated union)
import { Schema } from "effect"
const Shape = Schema.TaggedUnion({
Circle: { radius: Schema.Number },
Rectangle: { width: Schema.Number, height: Schema.Number }
})
// Pattern-match on a decoded value
const area = Shape.match({ _tag: "Circle", radius: 5 }, {
Circle: (c) => Math.PI * c.radius ** 2,
Rectangle: (r) => r.width * r.height
})
Type-level representation returned by
TaggedUnion
.
TaggedUnion<{ readonly [function (type parameter) KK in keyof function (type parameter) CasesByTag in TaggedUnion<const CasesByTag extends Record<string, Struct.Fields>>(casesByTag: CasesByTag): TaggedUnion<{ readonly [K in keyof CasesByTag & string]: TaggedStruct<K, CasesByTag[K]>; }>CasesByTag & string]: type TaggedStruct<
Tag extends SchemaAST.LiteralValue,
Fields extends Struct.Fields
> = Struct<{
[K in keyof ({
readonly _tag: tag<Tag>
} & Fields)]: ({
readonly _tag: tag<Tag>
} & Fields)[K]
}>
Creates a struct schema with an automatically populated _tag field.
When to use
Use to define a tagged union case from a literal tag and a set of fields.
Details
When using the make method, the _tag field is optional and will be
added automatically. However, when decoding or encoding, the _tag field
must be present in the input.
Example (Defining a tagged struct shorthand)
import { Schema } from "effect"
// Defines a struct with a fixed `_tag` field
const tagged = Schema.TaggedStruct("A", {
a: Schema.String
})
// This is the same as writing:
const equivalent = Schema.Struct({
_tag: Schema.tag("A"),
a: Schema.String
})
Example (Accessing the literal value of the tag)
import { Schema } from "effect"
const tagged = Schema.TaggedStruct("A", {
a: Schema.String
})
// literal: "A"
const literal = tagged.fields._tag.schema.literal
Type-level representation returned by
TaggedStruct
.
TaggedStruct<function (type parameter) KK, function (type parameter) CasesByTag in TaggedUnion<const CasesByTag extends Record<string, Struct.Fields>>(casesByTag: CasesByTag): TaggedUnion<{ readonly [K in keyof CasesByTag & string]: TaggedStruct<K, CasesByTag[K]>; }>CasesByTag[function (type parameter) KK]> }> {
const const cases: anycases: any = {}
const const members: anymembers: any = []
for (const const key: stringkey of 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(casesByTag: const CasesByTag extends Record<string, Struct.Fields>casesByTag)) {
const members: anymembers.push(const cases: anycases[const key: stringkey] = function TaggedStruct<
Tag extends SchemaAST.LiteralValue,
Fields extends Struct.Fields
>(
value: Tag,
fields: Fields
): TaggedStruct<Tag, Fields>
Creates a struct schema with an automatically populated _tag field.
When to use
Use to define a tagged union case from a literal tag and a set of fields.
Details
When using the make method, the _tag field is optional and will be
added automatically. However, when decoding or encoding, the _tag field
must be present in the input.
Example (Defining a tagged struct shorthand)
import { Schema } from "effect"
// Defines a struct with a fixed `_tag` field
const tagged = Schema.TaggedStruct("A", {
a: Schema.String
})
// This is the same as writing:
const equivalent = Schema.Struct({
_tag: Schema.tag("A"),
a: Schema.String
})
Example (Accessing the literal value of the tag)
import { Schema } from "effect"
const tagged = Schema.TaggedStruct("A", {
a: Schema.String
})
// literal: "A"
const literal = tagged.fields._tag.schema.literal
TaggedStruct(const key: stringkey, casesByTag: const CasesByTag extends Record<string, Struct.Fields>casesByTag[const key: stringkey]))
}
const const union: Union<any>const union: {
Type: { [K in keyof Members]: Members[K]["Type"]; }[number];
Encoded: { [K in keyof Members]: Members[K]["Encoded"]; }[number];
DecodingServices: { [K in keyof Members]: Members[K]["DecodingServices"]; }[number];
EncodingServices: { [K in keyof Members]: Members[K]["EncodingServices"]; }[number];
Iso: { [K in keyof Members]: Members[K]["Iso"]; }[number];
members: Members;
mapMembers: (f: (members: any) => To, options?: { readonly unsafePreserveChecks?: boolean | undefined } | undefined) => Union<{ [K in keyof Readonly<To>]: Readonly<To>[K]; }>;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Annotations.Bottom<any, readonly []>) => Union<any>;
annotateKey: (annotations: Annotations.Key<any>) => Union<any>;
check: (checks_0: SchemaAST.Check<any>, ...checks: Array<SchemaAST.Check<any>>) => Union<any>;
rebuild: (ast: SchemaAST.Union<any>) => Union<any>;
make: (input: any, options?: MakeOptions) => any;
makeOption: (input: any, options?: MakeOptions) => Option_.Option<any>;
makeEffect: (input: any, options?: MakeOptions) => Effect.Effect<any, 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; <…;
}
union = function Union<
Members extends ReadonlyArray<Constraint>
>(
members: Members,
options?: { mode?: "anyOf" | "oneOf" }
): Union<Members>
Creates a union schema from an array of member schemas. Members are tested in
order; the first match is returned.
Details
Optionally, specify mode:
"anyOf" (default) — matches if any member matches.
"oneOf" — matches if exactly one member matches.
Example (Defining a string or number union)
import { Schema } from "effect"
const schema = Schema.Union([Schema.String, Schema.Number])
Schema.decodeUnknownSync(schema)("hello") // "hello"
Schema.decodeUnknownSync(schema)(42) // 42
Union(const members: anymembers)
const { const guards: {}guards, const isAnyOf: <const Keys>(
keys: readonly Keys[]
) => (value: any) => value is any
isAnyOf, const match: {
<Cases extends {}>(
value: any,
cases: Cases
): Cases[keyof Cases] extends (
value: any
) => infer R
? Unify<R>
: never
<Cases extends {}>(cases: Cases): (
value: any
) => Cases[keyof Cases] extends (
value: any
) => infer R
? Unify<R>
: never
}
match } = function toTaggedUnion<
Tag extends PropertyKey
>(
tag: Tag
): <
Members extends ReadonlyArray<
Constraint & {
readonly Type: {
readonly [K in Tag]: PropertyKey
}
}
>
>(
self: Union<Members>
) => toTaggedUnion<Tag, Members>
Augments an existing
Union
of tagged structs with utility methods keyed by the discriminant field.
Example (Adding tagged-union utilities to an existing union)
import { Schema } from "effect"
const A = Schema.TaggedStruct("A", { value: Schema.Number })
const B = Schema.TaggedStruct("B", { name: Schema.String })
const MyUnion = Schema.Union([A, B]).pipe(Schema.toTaggedUnion("_tag"))
// Pattern-match on the union
const result = MyUnion.match({ _tag: "A", value: 1 }, {
A: (a) => `number: ${a.value}`,
B: (b) => `name: ${b.name}`
})
toTaggedUnion("_tag")(const union: Union<any>const union: {
Type: { [K in keyof Members]: Members[K]["Type"]; }[number];
Encoded: { [K in keyof Members]: Members[K]["Encoded"]; }[number];
DecodingServices: { [K in keyof Members]: Members[K]["DecodingServices"]; }[number];
EncodingServices: { [K in keyof Members]: Members[K]["EncodingServices"]; }[number];
Iso: { [K in keyof Members]: Members[K]["Iso"]; }[number];
members: Members;
mapMembers: (f: (members: any) => To, options?: { readonly unsafePreserveChecks?: boolean | undefined } | undefined) => Union<{ [K in keyof Readonly<To>]: Readonly<To>[K]; }>;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Annotations.Bottom<any, readonly []>) => Union<any>;
annotateKey: (annotations: Annotations.Key<any>) => Union<any>;
check: (checks_0: SchemaAST.Check<any>, ...checks: Array<SchemaAST.Check<any>>) => Union<any>;
rebuild: (ast: SchemaAST.Union<any>) => Union<any>;
make: (input: any, options?: MakeOptions) => any;
makeOption: (input: any, options?: MakeOptions) => Option_.Option<any>;
makeEffect: (input: any, options?: MakeOptions) => Effect.Effect<any, 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; <…;
}
union)
return const make: <S extends Constraint>(
ast: S["ast"],
options?: object
) => S
Creates a schema from an AST (Abstract Syntax Tree) node.
Details
This is the fundamental constructor for all schemas in the Effect Schema
library. It takes an AST node and wraps it in a fully-typed schema that
preserves all type information and provides the complete schema API.
The make function is used internally to create all primitive schemas like
String, Number, Boolean, etc., as well as more complex schemas. It's
the bridge between the untyped AST representation and the strongly-typed
schema.
make(const union: Union<any>const union: {
Type: { [K in keyof Members]: Members[K]["Type"]; }[number];
Encoded: { [K in keyof Members]: Members[K]["Encoded"]; }[number];
DecodingServices: { [K in keyof Members]: Members[K]["DecodingServices"]; }[number];
EncodingServices: { [K in keyof Members]: Members[K]["EncodingServices"]; }[number];
Iso: { [K in keyof Members]: Members[K]["Iso"]; }[number];
members: Members;
mapMembers: (f: (members: any) => To, options?: { readonly unsafePreserveChecks?: boolean | undefined } | undefined) => Union<{ [K in keyof Readonly<To>]: Readonly<To>[K]; }>;
Rebuild: Rebuild;
ast: Ast;
annotate: (annotations: Annotations.Bottom<any, readonly []>) => Union<any>;
annotateKey: (annotations: Annotations.Key<any>) => Union<any>;
check: (checks_0: SchemaAST.Check<any>, ...checks: Array<SchemaAST.Check<any>>) => Union<any>;
rebuild: (ast: SchemaAST.Union<any>) => Union<any>;
make: (input: any, options?: MakeOptions) => any;
makeOption: (input: any, options?: MakeOptions) => Option_.Option<any>;
makeEffect: (input: any, options?: MakeOptions) => Effect.Effect<any, 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; <…;
}
union.Bottom<unknown, unknown, unknown, unknown, Union<any>, Union<any>, unknown, unknown, readonly [], unknown, "readonly", "required", "no-default", "readonly", "required">["ast"]: SchemaAST.Union<any>(property) Bottom<unknown, unknown, unknown, unknown, Union<any>, Union<any>, unknown, unknown, readonly [], unknown, "readonly", "required", "no-default", "readonly", "required">["ast"]: {
_tag: 'Union';
types: ReadonlyArray<A>;
mode: "anyOf" | "oneOf";
encodingChecks: Checks | undefined;
getParser: (recur: (ast: AST) => SchemaParser.Parser) => SchemaParser.Parser;
_rebuild: (recur: (ast: AST) => AST, checks: Checks | undefined, encodingChecks: Checks | undefined) => Union<AST>;
recur: (recur: (ast: AST) => AST) => Union<AST>;
flip: (recur: (ast: AST) => AST) => Union<AST>;
matchPart: (s: string, options: ParseOptions) => LiteralValue | undefined;
getExpected: (getExpected: (ast: AST) => string) => string;
annotations: Schema.Annotations.Annotations | undefined;
checks: Checks | undefined;
encoding: Encoding | undefined;
context: Context | undefined;
toString: () => string;
}
ast, { cases: anycases, isAnyOf: <const Keys>(
keys: readonly Keys[]
) => (value: any) => value is any
isAnyOf, guards: {}guards, match: {
<Cases extends {}>(
value: any,
cases: Cases
): Cases[keyof Cases] extends (
value: any
) => infer R
? Unify<R>
: never
<Cases extends {}>(cases: Cases): (
value: any
) => Cases[keyof Cases] extends (
value: any
) => infer R
? Unify<R>
: never
}
match })
}