API reference › @evolu/common › Redacted › Redacted

Defined in: packages/common/src/Redacted.ts:77

A wrapper type that prevents sensitive values from being accidentally exposed through logging, serialization, or inspection.

The wrapped value is hidden and can only be accessed explicitly via revealRedacted. All standard methods (toString, toJSON, and Node.js inspect) return <redacted>.

For type-level distinction between different secrets, use branded types.

Redacted guards against accidental exposure, such as logs, error reports, and serialized payloads. It does not hide the value from a debugger: DevTools and heap snapshots can still reach it.

A structured clone, such as a postMessage to a worker, does not copy the hidden value and arrives as an empty object. Reveal the value before posting it, and wrap it again on arrival if the receiver passes it to app code.

A wrapper equals only itself and has no hash, so neither can expose the value. Two wrappers of the same value are not equal; compare revealed values instead.

Implements Disposable, so the using syntax detaches the value when the scope ends. Disposal does not overwrite the value or release other references to it.

Example

import {
  assertErr,
  assertEqual,
  createRedacted,
  revealRedacted,
  trySync,
  type Brand,
  type Redacted,
} from "@evolu/common";

type ApiKey = string & Brand<"ApiKey">;
type DbPassword = string & Brand<"DbPassword">;
type RedactedApiKey = Redacted<ApiKey>;

// Apply brands only after validation or at another trusted boundary.
const apiKey: ApiKey = "secret-123" as ApiKey;
using redactedKey: RedactedApiKey = createRedacted(apiKey);
const fetchUser = (key: RedactedApiKey): ApiKey => revealRedacted(key);

assertEqual(redactedKey.toString(), "<redacted>");
assertEqual(JSON.stringify({ apiKey: redactedKey }), '{"apiKey":"<redacted>"}');
assertEqual(fetchUser(redactedKey), apiKey);

using password = createRedacted("password" as DbPassword);
// @ts-expect-error Redacted secrets retain their distinct branded types.
fetchUser(password);

const disposedKey = (() => {
  using key = createRedacted(apiKey);
  return key;
})();
// Leaving the `using` scope detaches the value, so revealing it throws.
assertErr(trySync(() => revealRedacted(disposedKey)));

Extends

Methods

[dispose]()

[dispose](): void;

Defined in: node_modules/@typescript/old/lib/lib.esnext.disposable.d.ts:34

Inherited from

Disposable.[dispose]

Properties

[___brand]

readonly [___brand]: Readonly<Record<B, true>>;

Defined in: packages/common/src/Brand.ts:76

Inherited from

Brand.[___brand]


toJSON

readonly toJSON: () => "<redacted>";

Defined in: packages/common/src/Redacted.ts:84


toString

readonly toString: () => "<redacted>";

Defined in: packages/common/src/Redacted.ts:83

Returns a string representation of an object.


Type

readonly Type: A;

Defined in: packages/common/src/Redacted.ts:82

The inner type. This is a type-only phantom property. Use it through typeof redacted.Type; it does not exist at runtime.