Data.ts overview
Base class for inspectable, pipeable data objects.
Mental model
ClassextendsPipeable.Classand implementsInspectable.Inspectable. It is the recommended root class for value objects ineffect-based packages.- Subclasses must implement
[idSymbol], returning either a string identifier (used as_idin the JSON view) or a function producing a fully custom string representation. Classdoes not implementEqual.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 returnRedactable.getRedacted(this)instead of the normal_id+ properties view, so[idSymbol]is never called on redacted instances.
Common tasks
- Define a value object: extend
Classand implementidSymbol - 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
toString
Returns a string representation of self
Signature
declare const toString: (self: { [idSymbol](): idSymbolResult }) => string
Model symbols
idSymbol
Symbol naming the id method that subclasses implement.
- When the method returns a string,
toStringandtoJSONuse it as the_idkey alongside the own enumerable properties of the instance. - When the method returns a function,
toStringandtoJSONreturn whatever that function produces, bound tothis.
Signature
declare const idSymbol: unique symbol
Models
Class (class)
Abstract base class providing Pipeable and Inspectable for value objects.
Signature
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
declare const [idSymbol]: () => idSymbolResult
toJSON (method)
Returns the JSON view of this, or its redacted representation if this implements Redactable
Signature
declare const toJSON: () => unknown
[Inspectable.NodeInspectSymbol] (method)
Hooks into Node.js util.inspect.
Signature
declare const [Inspectable.NodeInspectSymbol]: () => unknown
toString (method)
Returns the printable representation of this, or its redacted representation if this implements Redactable
Signature
declare const toString: () => string
Type (type alias)
Type of objects produced by this module.
Signature
type Type = Pipeable.Pipeable & Inspectable.Inspectable & { [idSymbol](): idSymbolResult }
idSymbolResult (type alias)
Result type of the [idSymbol] function
Signature
type idSymbolResult = string | (() => string)
Module markers
moduleTag
Module tag.
Signature
declare const moduleTag: "@parischap/effect-lib/Data/"