UiSchema unstable
Schemas for UI values that are built often and must stay cheap to build.
TaggedEnum takes the same input as Schema.TaggedUnion and attaches one
constructor per case, plus is and match, directly to the schema, the
way Data.taggedEnum does. The constructors skip validation, so a model or
message built in a hot path costs no more than an object literal, while the
same definition still decodes and encodes as a schema.
Example A message union with direct constructors
import { UiSchema } from "effect/ui"import { Schema } from "effect"
const Message = UiSchema.TaggedEnum({ Increment: { by: Schema.Number }, Reset: {}})
Message.Increment({ by: 1 }) // => { by: 1, _tag: "Increment" }Message.Reset() // => { _tag: "Reset" }Constructors
TaggedEnum
unstableBuilds a Schema.TaggedUnion and attaches one unchecked constructor per
case, plus is and match, directly to the schema.
When to use
Use when models or messages are built in hot paths from trusted values and the same definition must still decode and encode as a schema.
Details
Each constructor is keyed by its tag and only adds _tag to its input, like
Data.taggedEnum. The input is the case's Type without _tag; a case
with no other fields takes no argument. is(tag) returns a guard that
checks only _tag, and match matches exhaustively, data-first or
data-last. The Schema.TaggedUnion members, cases included, are kept,
and annotate returns a schema that still has the constructors.
Gotchas
- Constructors skip validation, checks and constructor defaults. A field
with a constructor default is required, and a branded field takes a
branded value. Use
cases[tag].makewhen you need validation or defaults. is(tag)doesn't look past_tag. Decode untrusted input first.- A tag in TaggedEnum.ReservedTag (
ast,annotate,cases,is,match,make,pipe,name,Type, ...) is a type error. - A case can't declare its own
_tagfield, because the case key is the tag. This is a type error, and throws when the schema is built. - A numeric key becomes a string tag:
{ 1: {} }has the tag"1".
Signature
declare const TaggedEnum: <CasesByTag extends Record<string, Fields>>(casesByTag: CasesByTag & { [K in string | number | symbol]: K extends ReservedTag ? never : { readonly _tag?: undefined;} }) => TaggedEnum<{ [K in string | number | symbol]: TaggedStruct<TagOf<K>, CasesByTag[K]> }>Example Building, matching and decoding
import { UiSchema } from "effect/ui"import { Schema } from "effect"
const Shape = UiSchema.TaggedEnum({ Circle: { radius: Schema.Number }, Rect: { width: Schema.Number, height: Schema.Number }})
const circle = Shape.Circle({ radius: 1 })
Shape.is("Circle")(circle) // => true
Shape.match(circle, { Circle: ({ radius }) => radius, Rect: ({ width, height }) => width * height}) // => 1
Schema.decodeUnknownSync(Shape)({ _tag: "Rect", width: 2, height: 3 }) // => { _tag: "Rect", width: 2, height: 3 }Models
A Schema.TaggedUnion with one unchecked constructor per case, is and
match.
Details
The schema members come from TaggedEnum.Base and the constructors from TaggedEnum.Constructors.
Signature
type TaggedEnum<Cases extends Record<string, Schema.Constraint>> = TaggedEnum.Base<Cases> & TaggedEnum.Constructors<Cases>Other
TaggedEnum
unstableTypes that make up a TaggedEnum.