Skip to main content Link Search Menu Expand Document (external link)

Data overview

Base class for inspectable, pipeable data objects.

Mental model

  • Class extends Pipeable.Class and implements Inspectable.Inspectable. It is the recommended root class for value objects in Effect-based packages.
  • Subclasses must implement [idSymbol], returning either a string identifier (used as _id in the JSON view) or a function producing a fully custom string representation.
  • Class does not implement Equal.Equal; for value-based equality, extend {@link “./EquivalenceBasedEqualityData.js” | MEquivalenceBasedEqualityData.Class} instead.

Common tasks

  • Define a value object: extend {@link Class} and implement {@link idSymbol}
  • Customize printing: return a function from [idSymbol]

Quickstart

Example (Defining a value object)

import * as MData from "@parischap/effect-lib/MData"

class Point extends MData.Class {
  constructor(
    readonly x: number,
    readonly y: number
  ) {
    super()
  }
  [MData.idSymbol]() {
    return "@example/Point/"
  }
}

console.log(new Point(1, 2).toJSON())
// { _id: '@example/Point/', x: 1, y: 2 }

Table of contents


Model symbols

idSymbol

Symbol naming the id method that subclasses implement.

  • When the method returns a string, toString and toJSON use it as the _id key alongside the own enumerable properties of the instance.
  • When the method returns a function, toString and toJSON return whatever that function produces, bound to this.

Signature

export declare const idSymbol: typeof idSymbol

Models

Class (class)

Abstract base class providing Pipeable and Inspectable for value objects.

Signature

export declare class Class

[idSymbol] (method)

Returns the id of this. A string identifier is used as the _id field of the JSON view; a function produces a fully custom representation when invoked with this bound.

Signature

abstract [idSymbol](): string | (() => string);

toJSON (method)

Returns the JSON view of this.

Signature

toJSON(): unknown

[Inspectable.NodeInspectSymbol] (method)

Hooks into Node.js util.inspect.

Signature

[Inspectable.NodeInspectSymbol](): unknown

toString (method)

Returns the printable representation of this.

Signature

override toString(): string

Type (type alias)

Type of objects produced by this module.

Signature

export type Type = Pipeable.Pipeable & Inspectable.Inspectable & { [idSymbol](): string | (() => string) }

Module markers

moduleTag

Module tag.

Signature

export declare const moduleTag: "@parischap/effect-lib/Data/"