InspectableA base prototype object that implements the Inspectable interface.
When to use
Use as a prototype for plain objects that should share standard inspectable behavior.
Details
This object provides default implementations for the Inspectable methods. It can be used as a prototype for objects that want to be inspectable, or as a mixin to add inspection capabilities to existing objects.
Example (Using the base inspectable prototype)
import { Inspectable } from "effect"
// Use as prototype
const myObject = Object.create(Inspectable.BaseProto)
myObject.name = "example"
myObject.value = 42
console.log(myObject.toString()) // Pretty printed representation
// Or extend in a constructor
function MyClass(this: any, name: string) {
this.name = name
}
MyClass.prototype = Object.create(Inspectable.BaseProto)
MyClass.prototype.constructor = MyClassexport const const BaseProto: Inspectableconst BaseProto: {
toString: () => string;
toJSON: () => unknown;
}
A base prototype object that implements the
Inspectable
interface.
When to use
Use as a prototype for plain objects that should share standard inspectable behavior.
Details
This object provides default implementations for the
Inspectable
methods.
It can be used as a prototype for objects that want to be inspectable,
or as a mixin to add inspection capabilities to existing objects.
Example (Using the base inspectable prototype)
import { Inspectable } from "effect"
// Use as prototype
const myObject = Object.create(Inspectable.BaseProto)
myObject.name = "example"
myObject.value = 42
console.log(myObject.toString()) // Pretty printed representation
// Or extend in a constructor
function MyClass(this: any, name: string) {
this.name = name
}
MyClass.prototype = Object.create(Inspectable.BaseProto)
MyClass.prototype.constructor = MyClass
BaseProto: Inspectable = {
Inspectable.toJSON(): unknowntoJSON() {
return const toJson: (input: unknown) => unknownConverts a value to a JSON-serializable representation safely.
When to use
Use when you need a safe, JSON-serializable representation of a value
without risking unhandled errors.
Details
This function attempts to extract JSON data from objects that implement the
toJSON method, recursively processes arrays, and handles errors gracefully.
For objects that don't have a toJSON method, it applies redaction to
protect sensitive information.
toJson(this)
},
[const NodeInspectSymbol: typeof NodeInspectSymbolDefines the symbol used by Node.js for custom object inspection.
When to use
Use to implement Node.js custom inspection for a value.
Details
This symbol is recognized by Node.js's util.inspect() function and the REPL
for custom object representation. When an object has a method with this symbol,
it will be called to determine how the object should be displayed.
Example (Defining custom Node inspection)
import { Inspectable } from "effect"
class CustomObject {
constructor(private value: string) {}
[Inspectable.NodeInspectSymbol]() {
return `CustomObject(${this.value})`
}
}
const obj = new CustomObject("hello")
console.log(obj) // Displays: CustomObject(hello)
The type of the Node.js inspection symbol used for custom object inspection.
This symbol type is used to implement custom inspection behavior in Node.js
environments.
When to use
Use to type methods keyed by the Node.js custom inspection symbol.
Example (Typing custom Node inspection)
import { Inspectable } from "effect"
class CustomObject {
constructor(private value: string) {}
[Inspectable.NodeInspectSymbol]() {
return `CustomObject(${this.value})`
}
}
const obj = new CustomObject("test")
console.log(obj) // CustomObject(test)
NodeInspectSymbol]() {
return this.Inspectable.toJSON(): unknowntoJSON()
},
Inspectable.toString(): stringtoString() {
return function format(input: unknown, options?: {
readonly space?: number | string | undefined;
readonly ignoreToString?: boolean | undefined;
}): string
Converts any JavaScript value into a human-readable string.
When to use
Use when you need to format arbitrary JavaScript values for debugging,
logging, or error messages.
Details
- Output is not valid JSON; use
formatJson
when you need
parseable JSON.
- Handles
BigInt, Symbol, Set, Map, Date, RegExp, and class
instances that JSON.stringify cannot represent.
- Circular references are shown as
"[Circular]" instead of throwing.
- Primitives: stringified naturally (
null, undefined, 123, true).
Strings are JSON-quoted.
- Objects with a custom
toString (not Object.prototype.toString):
toString() is called unless ignoreToString is true.
- Errors with a
cause: formatted as "<message> (cause: <cause>)".
- Iterables (
Set, Map, etc.): formatted as
ClassName([...elements]).
- Class instances: wrapped as
ClassName({...}).
Redactable values are automatically redacted.
- Arrays/objects with 0–1 entries are inline; larger ones are
pretty-printed when
space is set.
space — indentation unit (number of spaces, or a string like
"\t"). Defaults to 0 (compact).
ignoreToString — skip calling toString(). Defaults to false.
Example (Formatting compact output)
import { Formatter } from "effect"
console.log(Formatter.format({ a: 1, b: [2, 3] }))
// {"a":1,"b":[2,3]}
Example (Pretty-printed output)
import { Formatter } from "effect"
console.log(Formatter.format({ a: 1, b: [2, 3] }, { space: 2 }))
// {
// "a": 1,
// "b": [
// 2,
// 3
// ]
// }
Example (Handling circular references)
import { Formatter } from "effect"
const obj: any = { name: "loop" }
obj.self = obj
console.log(Formatter.format(obj))
// {"name":"loop","self":[Circular]}
format(this.Inspectable.toJSON(): unknowntoJSON())
}
}