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 }export interface 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 tag<Tag extends SchemaAST.LiteralValue>Tag extends import SchemaASTSchemaAST.type LiteralValue =
| string
| number
| bigint
| boolean
The set of primitive types that can appear as a
Literal
value.
LiteralValue> extends interface withConstructorDefault<S extends Constraint & WithoutConstructorDefault>Attaches a constructor default value to a schema field.
Details
Constructor defaults are applied only during make*, not during decoding or
encoding.
Example (Defining an optional field with a static default)
import { Effect, Schema } from "effect"
const MySchema = Schema.Struct({
name: Schema.String.pipe(
Schema.optionalKey,
Schema.withConstructorDefault(Effect.succeed("anonymous"))
)
})
const value = MySchema.make({})
// value: { name: "anonymous" }
Type-level representation returned by
withConstructorDefault
.
withConstructorDefault<interface Literal<L extends SchemaAST.LiteralValue>Creates a schema for a single literal value (string, number, bigint, boolean, or null).
Example (Defining a string literal)
import { Schema } from "effect"
const schema = Schema.Literal("hello")
// Type: Schema.Literal<"hello">
Type-level representation returned by
Literal
.
Literal<function (type parameter) Tag in tag<Tag extends SchemaAST.LiteralValue>Tag>> {}
/**
* Combines a {@link Literal} schema with {@link 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)
*
* ```ts
* 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 }
* ```
*
* @see {@link tagDefaultOmit} to also omit the tag during encoding
* @see {@link TaggedStruct} for a shorthand that adds `_tag` automatically
* @category constructors
* @since 3.10.0
*/
export function 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<function (type parameter) Tag in tag<Tag extends SchemaAST.LiteralValue>(literal: Tag): tag<Tag>Tag extends import SchemaASTSchemaAST.type LiteralValue =
| string
| number
| bigint
| boolean
The set of primitive types that can appear as a
Literal
value.
LiteralValue>(literal: Tag extends SchemaAST.LiteralValueliteral: function (type parameter) Tag in tag<Tag extends SchemaAST.LiteralValue>(literal: Tag): tag<Tag>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 tag<Tag extends SchemaAST.LiteralValue>(literal: Tag): tag<Tag>Tag> {
return function Literal<
L extends SchemaAST.LiteralValue
>(literal: L): Literal<L>
Creates a schema for a single literal value (string, number, bigint, boolean, or null).
Example (Defining a string literal)
import { Schema } from "effect"
const schema = Schema.Literal("hello")
// Type: Schema.Literal<"hello">
Literal(literal: Tag extends SchemaAST.LiteralValueliteral).pipe(function withConstructorDefault<
S extends Constraint & WithoutConstructorDefault
>(
defaultValue: Effect.Effect<
S["~type.make.in"],
SchemaError
>
): (schema: S) => withConstructorDefault<S>
Attaches a constructor default value to a schema field.
Details
Constructor defaults are applied only during make*, not during decoding or
encoding.
Example (Defining an optional field with a static default)
import { Effect, Schema } from "effect"
const MySchema = Schema.Struct({
name: Schema.String.pipe(
Schema.optionalKey,
Schema.withConstructorDefault(Effect.succeed("anonymous"))
)
})
const value = MySchema.make({})
// value: { name: "anonymous" }
withConstructorDefault(import EffectEffect.succeed(literal: SchemaAST.LiteralValueliteral)))
}