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.literalexport type 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) Tag in type TaggedStruct<Tag extends SchemaAST.LiteralValue, Fields extends Struct.Fields>Tag extends import SchemaASTSchemaAST.type LiteralValue =
| string
| number
| bigint
| boolean
The set of primitive types that can appear as a
Literal
value.
LiteralValue, function (type parameter) Fields in type TaggedStruct<Tag extends SchemaAST.LiteralValue, Fields extends Struct.Fields>Fields extends 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> = interface Struct<Fields extends 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 }
Namespace for struct field type utilities.
Details
These types compute the decoded Type, encoded Encoded, and constructor
input MakeIn of a
Struct
from its field map, handling optional,
mutable, and other field modifiers automatically.
Struct.Fields — constraint for the field map object
Struct.Type<F> — decoded type of the struct
Struct.Encoded<F> — encoded type of the struct
Struct.MakeIn<F> — constructor input (optional/defaulted fields may be omitted)
Struct.DecodingServices<F> / Struct.EncodingServices<F> — required services
Type-level representation returned by
Struct
.
Struct<
type Simplify<T> = { [K in keyof T]: T[K]; }Flattens intersection types into a single object type for readability.
When to use
Use when hovering over a type shows A & B & C instead of the merged shape.
Details
This helper is purely cosmetic at the type level and has no runtime effect.
It preserves readonly modifiers; use
Mutable
to strip them.
Example (Flattening an intersection)
import type { Struct } from "effect"
type Original = { a: string } & { b: number }
// Without Simplify, the type displays as `{ a: string } & { b: number }`
type Simplified = Struct.Simplify<Original>
// { a: string; b: number }
Simplify<{ readonly _tag: tag<Tag>(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: Annotations.Bottom<Tag, readonly []>) => withConstructorDefault<Literal<Tag>>;
annotateKey: (annotations: Annotations.Key<Tag>) => withConstructorDefault<Literal<Tag>>;
check: (checks_0: SchemaAST.Check<Tag>, ...checks: Array<SchemaAST.Check<Tag>>) => withConstructorDefault<Literal<Tag>>;
rebuild: (ast: SchemaAST.Literal) => withConstructorDefault<Literal<Tag>>;
make: (input: Tag, options?: MakeOptions) => Tag;
makeOption: (input: Tag, options?: MakeOptions) => Option_.Option<Tag>;
makeEffect: (input: Tag, options?: MakeOptions) => Effect.Effect<Tag, 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: interface tag<Tag extends SchemaAST.LiteralValue>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 }
Type-level representation returned by
tag
.
tag<function (type parameter) Tag in type TaggedStruct<Tag extends SchemaAST.LiteralValue, Fields extends Struct.Fields>Tag> } & function (type parameter) Fields in type TaggedStruct<Tag extends SchemaAST.LiteralValue, Fields extends Struct.Fields>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)
*
* ```ts
* 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)
*
* ```ts
* import { Schema } from "effect"
*
* const tagged = Schema.TaggedStruct("A", {
* a: Schema.String
* })
*
* // literal: "A"
* const literal = tagged.fields._tag.schema.literal
* ```
*
* @category constructors
* @since 3.10.0
*/
export function 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 function (type parameter) Tag in TaggedStruct<const Tag extends SchemaAST.LiteralValue, const Fields extends Struct.Fields>(value: Tag, fields: Fields): TaggedStruct<Tag, Fields>Tag extends import SchemaASTSchemaAST.type LiteralValue =
| string
| number
| bigint
| boolean
The set of primitive types that can appear as a
Literal
value.
LiteralValue, const function (type parameter) Fields in TaggedStruct<const Tag extends SchemaAST.LiteralValue, const Fields extends Struct.Fields>(value: Tag, fields: Fields): TaggedStruct<Tag, Fields>Fields extends 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>(
value: const Tag extends SchemaAST.LiteralValuevalue: function (type parameter) Tag in TaggedStruct<const Tag extends SchemaAST.LiteralValue, const Fields extends Struct.Fields>(value: Tag, fields: Fields): TaggedStruct<Tag, Fields>Tag,
fields: const Fields extends Struct.Fieldsfields: function (type parameter) Fields in TaggedStruct<const Tag extends SchemaAST.LiteralValue, const Fields extends Struct.Fields>(value: Tag, fields: Fields): TaggedStruct<Tag, Fields>Fields
): 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) Tag in TaggedStruct<const Tag extends SchemaAST.LiteralValue, const Fields extends Struct.Fields>(value: Tag, fields: Fields): TaggedStruct<Tag, Fields>Tag, function (type parameter) Fields in TaggedStruct<const Tag extends SchemaAST.LiteralValue, const Fields extends Struct.Fields>(value: Tag, fields: Fields): TaggedStruct<Tag, Fields>Fields> {
return 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: tag<Tag>(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: Annotations.Bottom<Tag, readonly []>) => withConstructorDefault<Literal<Tag>>;
annotateKey: (annotations: Annotations.Key<Tag>) => withConstructorDefault<Literal<Tag>>;
check: (checks_0: SchemaAST.Check<Tag>, ...checks: Array<SchemaAST.Check<Tag>>) => withConstructorDefault<Literal<Tag>>;
rebuild: (ast: SchemaAST.Literal) => withConstructorDefault<Literal<Tag>>;
make: (input: Tag, options?: MakeOptions) => Tag;
makeOption: (input: Tag, options?: MakeOptions) => Option_.Option<Tag>;
makeEffect: (input: Tag, options?: MakeOptions) => Effect.Effect<Tag, 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: 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(value: const Tag extends SchemaAST.LiteralValuevalue), ...fields: const Fields extends Struct.Fieldsfields })
}