Hyperlinkv0.8.0-beta.28

Data

Data.TaggedClassconsteffect/Data.ts:96
<Tag extends string>(tag: Tag): new <A extends Record<string, any> = {}>(
  args: Types.VoidIfEmpty<{
    readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]
  }>
) => Readonly<A> & { readonly _tag: Tag } & Pipeable.Pipeable

Provides a base class for immutable data types with a _tag discriminator.

When to use

Use when you need a single-variant tagged type or an ad-hoc discriminator.

Details

Like Class, but the resulting instances also carry a readonly _tag: Tag property. The _tag is excluded from the constructor argument.

Example (Defining a tagged class)

import { Data } from "effect"

class Person extends Data.TaggedClass("Person")<{
  readonly name: string
}> {}

const mike = new Person({ name: "Mike" })
console.log(mike._tag)
// "Person"
Source effect/Data.ts:968 lines
export const TaggedClass = <Tag extends string>(
  tag: Tag
): new<A extends Record<string, any> = {}>(
  args: Types.VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P] }>
) => Readonly<A> & { readonly _tag: Tag } & Pipeable.Pipeable =>
  class extends Class {
    readonly _tag = tag
  } as any
Referenced by 1 symbols