A small, safe pattern for unknown TypeScript catch variables
A catch block is an uncertainty boundary. JavaScript permits throwing far more than Error instances: a dependency or application path can throw a string, a plain object, a number, or null. Code that immediately reads a caught value’s message assumes a shape that has not been established, so the formatter itself can fail while reporting the original problem.
The implementation keeps the responsibility small. Enable TypeScript’s useUnknownInCatchVariables option so a catch variable is treated as unknown. Send that value to one formatter rather than accessing error-specific properties throughout the application. The formatter first narrows with instanceof Error, which makes reading the normal Error message safe in that branch. It then handles strings as their own known case and uses a conservative fallback for every remaining value.
The wrapper around an operation returns either its normal result or a consistent failure shape containing the formatted message. That means callers do not need to know whether the failure began as an Error, a string, or null. Expected behavior is straightforward: a normal operation remains successful; a real Error preserves its message; a thrown string remains visible; and arbitrary values receive a safe description instead of triggering another exception.
This is deliberately not a universal error model. instanceof Error confirms that the value is an Error instance, including ordinary subclasses, but it does not establish that the message is actionable, suitable for users, or safe to expose. Applications that need domain codes, retry decisions, or user-facing copy still need policies after narrowing.
There is also an important proof boundary. useUnknownInCatchVariables is a compile-time guardrail: it prevents unproven property access in TypeScript. Runtime checks and tests establish how the formatter behaves for selected thrown values. Neither replaces the other, and neither makes every external error trustworthy. The useful baseline is simply to classify first, format second, and retain a fallback for what JavaScript can actually throw.
Working example
// TypeScript counterpart: enable `useUnknownInCatchVariables: true`; then `err` is // `unknown` here and must be narrowed before reading `err.message`. // This runnable JavaScript demonstrates runtime behavior only; it does not prove // TypeScript's compile-time narrowing. /** * Converts any value that was thrown into a display-safe message. * @param {unknown} err * @returns {string} */ export function formatCaughtError(err) { if (err instanceof Error) { return `Error: ${err.message}`; } if (typeof err === 'string') { return `Thrown string: ${err}`; } return `Thrown non-Error value (${err === null ? 'null' : typeof err})`; } /** * Executes an operation at an exception boundary and formats any thrown value. * @param {() => unknown} operation * @returns {{ ok: true, value: unknown } | { ok: false, message: string }} */ export function runAndFormat(operation) { try { return { ok: true, value: operation() }; } catch (err) { return { ok: false, message: formatCaughtError(err) }; } } Read the full article: https://chriseugenerodriguez.com/blog/safely-handle-unknown-catch-variables