Hyperlinkv0.8.0-beta.28

Schema

Schema.TaggedErrorClassconsteffect/Schema.ts:12981
<Self = never, Brand = {}>(identifier?: string): {
  <Tag extends string, const Fields extends Struct.Fields>(
    tag: Tag,
    fields: Fields,
    annotations?: Annotations.Declaration<
      Self,
      readonly [TaggedStruct<Tag, Fields>]
    >
  ): [Self] extends [never]
    ? MissingSelfGeneric<"Schema.TaggedErrorClass">
    : Class<
        Self,
        TaggedStruct<Tag, Fields>,
        Cause_.YieldableError & Brand
      >
  <Tag extends string, S extends Struct<Struct.Fields>>(
    tag: Tag,
    schema: S,
    annotations?: Annotations.Declaration<
      Self,
      readonly [
        Struct<Simplify<{ readonly _tag: tag<Tag> } & S["fields"]>>
      ]
    >
  ): [Self] extends [never]
    ? MissingSelfGeneric<"Schema.TaggedErrorClass">
    : Class<
        Self,
        Struct<Simplify<{ readonly _tag: tag<Tag> } & S["fields"]>>,
        Cause_.YieldableError & Brand
      >
}

Defines a schema-backed yieldable error class with an automatically populated _tag field.

When to use

Use to define typed errors that are schema validated, yielded in Effect.gen, and matched as tagged union members.

Example (Defining a tagged error class)

import { Effect, Schema } from "effect"

class NotFound extends Schema.TaggedErrorClass<NotFound>()("NotFound", {
  id: Schema.Number
}) {}

const program = Effect.gen(function*() {
  yield* new NotFound({ id: 42 })
})
constructors
Source effect/Schema.ts:1298135 lines
export const TaggedErrorClass: {
  <Self = never, Brand = {}>(identifier?: string): {
    <Tag extends string, const Fields extends Struct.Fields>(
      tag: Tag,
      fields: Fields,
      annotations?: Annotations.Declaration<Self, readonly [TaggedStruct<Tag, Fields>]>
    ): [Self] extends [never] ? MissingSelfGeneric<"Schema.TaggedErrorClass">
      : Class<Self, TaggedStruct<Tag, Fields>, Cause_.YieldableError & Brand>
    <Tag extends string, S extends Struct<Struct.Fields>>(
      tag: Tag,
      schema: S,
      annotations?: Annotations.Declaration<
        Self,
        readonly [Struct<Simplify<{ readonly _tag: tag<Tag> } & S["fields"]>>]
      >
    ): [Self] extends [never] ? MissingSelfGeneric<"Schema.TaggedErrorClass">
      : Class<Self, Struct<Simplify<{ readonly _tag: tag<Tag> } & S["fields"]>>, Cause_.YieldableError & Brand>
  }
} = (identifier?: string) => {
  return (
    tagValue: string,
    schema: Struct.Fields | Struct<Struct.Fields>,
    annotations?: Annotations.Declaration<any, readonly [Struct<Struct.Fields>]>
  ): any => {
    const struct = isStruct(schema) ?
      schema.mapFields((fields) => ({ _tag: tag(tagValue), ...fields }), {
        unsafePreserveChecks: true
      }) :
      TaggedStruct(tagValue, schema)
    return ErrorClass<any, {}>(identifier ?? tagValue)(
      struct,
      annotations as Annotations.Declaration<any, readonly [typeof struct]>
    )
  }
}
Referenced by 4 symbols