(u: unknown, whitespace?: number | string | undefined): stringConverts an unknown value to a string for diagnostics.
When to use
Use to produce a diagnostic string from a value whose runtime type is unknown.
Details
Strings are returned unchanged. Objects are formatted as JSON using the
provided whitespace setting when possible, and values that cannot be
formatted are converted with String.
export const const toStringUnknown: (
u: unknown,
whitespace?: number | string | undefined
) => string
Converts an unknown value to a string for diagnostics.
When to use
Use to produce a diagnostic string from a value whose runtime type is unknown.
Details
Strings are returned unchanged. Objects are formatted as JSON using the
provided whitespace setting when possible, and values that cannot be
formatted are converted with String.
toStringUnknown = (u: unknownu: unknown, whitespace: string | number | undefinedwhitespace: number | string | undefined = 2): string => {
if (typeof u: unknownu === "string") {
return u: stringu
}
try {
return typeof u: unknownu === "object" ? function formatJson(input: unknown, options?: {
readonly space?: number | string | undefined;
}): string
Stringifies a value to JSON safely, silently dropping circular references.
When to use
Use when you need valid JSON output, unlike format, and the input may
contain circular references that should be silently omitted rather than
throwing a TypeError.
Details
Uses JSON.stringify internally with a replacer that tracks the current
object ancestry. Circular references are replaced with undefined, which
omits them from object output. Redactable values are automatically redacted
before serialization. Values not supported by JSON, such as BigInt,
Symbol, undefined, and functions, follow standard JSON.stringify
behavior. The space parameter controls indentation and defaults to 0.
Example (Formatting compact JSON)
import { Formatter } from "effect"
console.log(Formatter.formatJson({ name: "Alice", age: 30 }))
// {"name":"Alice","age":30}
Example (Handling circular references)
import { Formatter } from "effect"
const obj: any = { name: "test" }
obj.self = obj
console.log(Formatter.formatJson(obj))
// {"name":"test"}
Example (Pretty-printed JSON)
import { Formatter } from "effect"
console.log(Formatter.formatJson({ name: "Alice", age: 30 }, { space: 2 }))
// {
// "name": "Alice",
// "age": 30
// }
formatJson(u: object | nullu, { space?: string | number | undefinedspace: whitespace: string | numberwhitespace }) : var String: StringConstructor
;(value?: any) => string
Allows manipulation and formatting of text strings and determination and location of substrings within strings.
String(u: {} | undefinedu)
} catch {
return var String: StringConstructor
;(value?: any) => string
Allows manipulation and formatting of text strings and determination and location of substrings within strings.
String(u: unknownu)
}
}