API reference / @evolu/common / Redacted / Redacted

Interface: Redacted<A>

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

Experimental

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.

The actual value lives in a WeakMap, so it never appears as a property and is automatically garbage collected when the wrapper is dropped. This is better than a class with a private field because private fields are still visible in devtools. Symbols can't be used because they don't support custom toString.

Implements Disposable for automatic cleanup via the using syntax.

Example

// Define branded types for your secrets
type ApiKey = string & Brand<"ApiKey">;
type DbPassword = string & Brand<"DbPassword">;

// Wrap them with Redacted for safe passing
type RedactedApiKey = Redacted<ApiKey>;
type RedactedDbPassword = Redacted<DbPassword>;

// Create a redacted secret
const apiKey: ApiKey = "secret-123" as ApiKey;
const redactedKey: RedactedApiKey = createRedacted(apiKey);

console.log(redactedKey); // <redacted>
console.log(revealRedacted(redactedKey)); // secret-123

// Type safety: RedactedApiKey ≠ RedactedDbPassword
const fetchUser = (key: RedactedApiKey) => {
  const value: ApiKey = revealRedacted(key);
  // use value...
};

fetchUser(redactedKey); // ✅
// fetchUser(createRedacted("x" as DbPassword)); // ❌ type error

// Automatic cleanup with `using`
{
  using secret = createRedacted("sensitive" as ApiKey);
  // ... use secret ...
} // automatically wiped from memory

Extends

  • Brand<"Redacted">.Disposable

Type Parameters

Type Parameter
A

Properties

PropertyModifierTypeDescriptionInherited fromDefined in
[___brand]readonlyReadonly<Record<B, true>>ExperimentalBrand.[___brand]packages/common/src/Brand.ts:60
TypereadonlyAExperimental The inner type. Useful for inference via typeof redacted.Type.-packages/common/src/Redacted.ts:61

Methods

[dispose]()

Defined in: node_modules/typescript/lib/lib.esnext.disposable.d.ts:36

Experimental

Returns

void

Inherited from
Disposable.[dispose]

Defined in: node_modules/@types/node/compatibility/disposable.d.ts:9

Experimental

Returns

void

Inherited from
Disposable.[dispose]

Was this page helpful?