Skip to content

Function: safeString()

API / @xmachines/core / utils / safeString

function safeString(value): string;

Defined in: packages/core/src/utils/describe-value.ts:19

Converts an unknown value into a string, and it trusts that value not.

String() itself throws for an object with a null prototype, and for a value whose toString or Symbol.toPrimitive throws. A caller in an error path runs this function, and a second throw there replaces the real failure with a TypeError. The function therefore falls back to the brand, and then to a fixed text.

Parameters

ParameterTypeDescription
valueunknownThe value to convert.

Returns

string

The text of the value. The function throws never.

Example

import { safeString } from "@xmachines/core/utils";
safeString(Object.create(null)); // "[object Object]"