<Tag extends SchemaAST.LiteralValue>(
literal: Tag
): withDecodingDefaultKey<tag<Tag>, never>Creates a literal _tag schema that is omitted from encoded output.
When to use
Use to decode data that omits the discriminator field while still constructing
values with a _tag for tagged union matching.
Details
The tag is filled during decoding and construction, like tag, but is omitted when encoding.
Example (Omitting tags during encoding)
import { Schema } from "effect"
const A = Schema.Struct({
_tag: Schema.tagDefaultOmit("A"),
value: Schema.Number
})
// Encode strips the _tag field
const encoded = Schema.encodeUnknownSync(A)({ _tag: "A", value: 1 })
// encoded: { value: 1 }export function function tagDefaultOmit<
Tag extends SchemaAST.LiteralValue
>(
literal: Tag
): withDecodingDefaultKey<tag<Tag>, never>
Creates a literal _tag schema that is omitted from encoded output.
When to use
Use to decode data that omits the discriminator field while still constructing
values with a _tag for tagged union matching.
Details
The tag is filled during decoding and construction, like
tag
, but is
omitted when encoding.
Example (Omitting tags during encoding)
import { Schema } from "effect"
const A = Schema.Struct({
_tag: Schema.tagDefaultOmit("A"),
value: Schema.Number
})
// Encode strips the _tag field
const encoded = Schema.encodeUnknownSync(A)({ _tag: "A", value: 1 })
// encoded: { value: 1 }
tagDefaultOmit<function (type parameter) Tag in tagDefaultOmit<Tag extends SchemaAST.LiteralValue>(literal: Tag): anyTag 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 tagDefaultOmit<Tag extends SchemaAST.LiteralValue>(literal: Tag): anyTag) {
return 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(literal: Tag extends SchemaAST.LiteralValueliteral).pipe(function withDecodingDefaultKey<
S extends Constraint,
R = never
>(
defaultValue: Effect.Effect<
S["Encoded"],
SchemaError,
R
>,
options?: DecodingDefaultOptions
): (self: S) => withDecodingDefaultKey<S, R>
Makes a struct key optional on the Encoded side and provides a default
Encoded value when the key is missing during decoding.
Details
The key uses optionalKey on the encoded side, so it may be absent from the
input object but not undefined. The default value is specified in terms
of the Encoded type (before any decoding transformations).
Options:
encodingStrategy:
"passthrough" (default): include the value in the encoded output.
"omit": omit the key from the encoded output.
Example (Providing a default for a missing struct key)
import { Effect, Schema } from "effect"
const MySchema = Schema.Struct({
name: Schema.String.pipe(Schema.withDecodingDefaultKey(Effect.succeed("anonymous")))
})
const result = Schema.decodeUnknownSync(MySchema)({})
// result: { name: "anonymous" }
withDecodingDefaultKey(import EffectEffect.succeed(literal: SchemaAST.LiteralValueliteral), { encodingStrategy?: "omit" | "passthrough" | undefinedencodingStrategy: "omit" }))
}