VIP-74: Serializable tagged unions (schema data)
Status: accepted 2026-11-27 (language ergonomics wave — twin of VIP-29 schema error for VIP-16 data).
Decision
Add schema data Name { VariantA; VariantB { field: Type } } as the serializable twin of data (VIP-16), parallel to how schema error (VIP-29) is the serializable twin of error (VIP-13).
Syntax
export schema data Shape {
Empty
Circle { readonly radius: number }
Rectangle { width: number; height?: number }
}schema data Nameuses the same two-word introducer rule asschema error Nameandschema service Name:schema,data, andNameappear on the same line without line breaks between them.schema data { … }andschema data = Tcontinue to declare an ordinaryschemanameddata.- Variants use VIP-16's variant syntax (
NameorName { fields }), separated by;,,, or line breaks. - Variant fields follow VIP-15's schema field mapping (
readonlystays immutable, writable fields wrap inSchema.mutableKey,?maps toSchema.optionalorSchema.optionalKeyunderexactOptionalPropertyTypes, and nested primitives/arrays/records/unions/schemas map identically toschema). export schema data Name { … }exports both theSchema.TaggedUnionconstant and itstypeof Name.Typetype companion.
Lowering & Declaration Output
export const Shape = Schema.TaggedUnion({
Empty: {},
Circle: { radius: Schema.Number },
Rectangle: {
width: Schema.mutableKey(Schema.Number),
height: Schema.mutableKey(Schema.optional(Schema.Number)),
},
})
export type Shape = typeof Shape.Type- Lowers directly to Effect 4's
Schema.TaggedUnion({ ... })plustype Name = typeof Name.Type, preserving 100% compatibility with Effect 4Schema.TaggedUnion(.cases,.guards,.isAnyOf,.match,.matchOrElse,Schema.decodeUnknownEffect,Schema.encodeEffect) and@effect/uimodels/messages. - Every variant's decoded type carries a literal
_tagdiscriminant, so VIP-7match (x)compile-time exhaustiveness checking (EK4701) works automatically withwhen { _tag: "Variant", ... }. - Declaration emit (
.d.tsand.d.vibe.ts) emitsexport declare const Name: Schema.TaggedUnion<...>;followed byexport type Name = typeof Name.Type;.
Static rules
| Mistake | Code |
|---|---|
A variant field named _tag | EK1601 |
export default schema data Name { … } | EK1603 |
Missing variant body (schema data Name) | EK1604 |
| Missing separator between variants | EK1605 |
Invalid variant name or parenthesized payload (Variant(x: number)) | EK1606 |
Type parameters (schema data Name<T> { … }) | EK1502 |
| Missing separator between variant fields | EK1504 |
| Non-field member inside a variant body (e.g. a method) | EK1505 |
Ambient declare schema data Name { … } | EK1506 |
| Unsupported field type or non-schema reference | EK2109 |
Motivation
Real Effect 4 and @effect/ui applications frequently define serializable discriminated unions (messages, API responses, persisted state machines). Previously, authors had to choose between data (not a Schema, no JSON encode/decode) or hand-writing const Name = Schema.TaggedUnion({ ... }); type Name = typeof Name.Type with repetitive Schema.* boilerplate. schema data completes the 2×2 matrix (error / schema error, data / schema data, service / schema service) with zero new runtime helpers.