Ch. 2 · TypeScript

TypeScript Error Handling Without Unsafe Casts

TypeScript Error Handling Without Unsafe Casts. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readadvancedupdated Oct 3, 2026

Thrown values can be anything, not only Error objects. Narrow catch values before reading messages or assigning domain-specific meaning.

Before you start

You should be comfortable with JavaScript values, functions and objects. Use a TypeScript project with strict checking enabled when trying the examples. Separate what the compiler proves from what must still be checked when values arrive at runtime.

The practical goal is to reason through this situation: Use instanceof Error for ordinary errors and validate custom error shapes where needed. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.

Step-by-step walkthrough

Step 1: Treat caught values as unknown

JavaScript can throw strings, objects or null. Reading message without narrowing is an assumption about code outside your control.

Step 2: Recognize supported cases

Use instanceof Error for ordinary errors and explicit shape checks for documented domain errors. Do not cast arbitrary failures into an HTTP response type.

Step 3: Separate public and diagnostic output

Show a controlled message to the user. Record internal details through an appropriate diagnostic path without exposing secrets or stack traces.

Worked scenario

Use instanceof Error for ordinary errors and validate custom error shapes where needed.

function errorMessage(error: unknown): string {
  if (error instanceof Error) return error.message;
  return 'An unexpected failure occurred';
}
console.log(errorMessage(new Error('Connection failed')));
console.log(errorMessage(null));
TypeScript

This helper safely accesses ordinary error messages but does not classify retryability or status codes. A public API may need a stricter mapping to prevent internal details from becoming user-facing text.

Common mistake

Casting every caught value to an HTTP error can fabricate status fields.

Verify the behavior

Throw an Error, string and null. Verify every case produces a defined message and no second exception; test known domain mappings separately from unexpected failures.

Interview exercise

Expose a safe public failure.

Answer and reasoning

Map validated known errors to expected responses and use a generic message plus internal diagnostics for unknown failures.

Continue learning

Compare the scenario with the TypeScript interview questions and test your understanding with the TypeScript MCQs. For terminology and implementation details, consult the reference material.

More in TypeScript

read ✓TypeScript · hard

TypeScript Abstract Classes and Contracts

Share behavior with abstract classes, enforce required members, and decide when an interface or composition is the better contract.

~2 min readread →
read ✓TypeScript · mid

TypeScript Enums vs Union Types

Compare enums with unions of string literals: runtime cost, serialization, exhaustiveness and which one fits application code.

~3 min readread →
esc