Type guard to check if an error has a message property. Use this to safely narrow unknown errors in catch blocks.
unknown
The error value (typically from a catch block)
True if the error has a string message property
try { await riskyOperation();} catch (error) { if (isErrorWithMessage(error)) { console.error(error.message); // TypeScript knows error has message }} Copy
try { await riskyOperation();} catch (error) { if (isErrorWithMessage(error)) { console.error(error.message); // TypeScript knows error has message }}
Type guard to check if an error has a message property. Use this to safely narrow
unknownerrors in catch blocks.