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

Data.ts 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 | MEquivalenceBasedEqualityData.Class`` instead.
  • If a subclass also implements Redactable.Redactable, toJSON, toString, and the Node.js inspection hook all return Redactable.getRedacted(this) instead of the normal _id + properties view, so [idSymbol] is never called on redacted instances.

Common tasks

  • Define a value object: extend Class and implement idSymbol
  • Customize printing: return a function from [idSymbol]
  • Protect sensitive fields from logs/JSON: also implement Redactable.Redactable

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 }

Exports Grouped by Category


Destructors

toJSON

Returns a JSON representation of self

Signature

declare const toJSON: (self: { [idSymbol](): idSymbolResult }) => unknown

Source

toString

Returns a string representation of self

Signature

declare const toString: (self: { [idSymbol](): idSymbolResult }) => string

Source

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

declare const idSymbol: unique symbol

Source

Models

Class (class)

Abstract base class providing Pipeable and Inspectable for value objects.

Signature

declare class Class

Source

[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

declare const [idSymbol]: () => idSymbolResult

Source

toJSON (method)

Returns the JSON view of this, or its redacted representation if this implements Redactable

Signature

declare const toJSON: () => unknown

Source

[Inspectable.NodeInspectSymbol] (method)

Hooks into Node.js util.inspect.

Signature

declare const [Inspectable.NodeInspectSymbol]: () => unknown

Source

toString (method)

Returns the printable representation of this, or its redacted representation if this implements Redactable

Signature

declare const toString: () => string

Source

Type (type alias)

Type of objects produced by this module.

Signature

type Type = Pipeable.Pipeable & Inspectable.Inspectable & { [idSymbol](): idSymbolResult }

Source

idSymbolResult (type alias)

Result type of the [idSymbol] function

Signature

type idSymbolResult = string | (() => string)

Source

Module markers

moduleTag

Module tag.

Signature

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

Source