0Pricing
TypeScript Academy · Lesson

Error typing (unknown), narrowing in catch

Type catch parameters as unknown, then narrow with instanceof/typeof or custom guards for safe error handling.

Error typing (unknown), narrowing in catch is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro

Goal: Catch errors as unknown and narrow safely. You will use instanceof, typeof, and custom type guards to read error data without surprises.

unknown + instanceof

Start with unknown. Narrow with instanceof Error to access message, stack, etc.

async function boom(): Promise<void> { throw new Error("boom") }

async function run() {
  try {
    await boom();
  } catch (e: unknown) {
    if (e instanceof Error) {
      console.error("Handled:", e.message);
    } else {
      console.error("Non-Error thrown:", e);
    }
  }
}

run();

typeof for primitives

Some code throws strings or other primitives. Use typeof to narrow and handle gracefully.

function throwString() { throw "bad" }

try {
  throwString();
} catch (e: unknown) {
  if (typeof e === "string") {
    console.warn("string error:", e.toUpperCase());
  } else {
    console.warn("other error", e);
  }
}

Custom guard

Create a user-defined type guard to recognize “error-like” shapes coming from various layers.

type ErrorLike = { message: string; code?: string };

function isErrorLike(e: unknown): e is ErrorLike {
  return !!e && typeof e === "object" && "message" in e;
}

try {
  throw { message: "oops", code: "E_BAD" };
} catch (e: unknown) {
  if (isErrorLike(e)) {
    console.error(e.message, e.code ?? "no-code");
  } else {
    console.error("unknown error", e);
  }
}

Safe logging

Centralize error handling. A tiny helper narrows and logs safely, avoiding crashes in logging itself.

function logErrorSafe(e: unknown): void {
  if (e instanceof Error) {
    console.error("Error:", e.message);
  } else if (typeof e === "string") {
    console.error("Error(string):", e);
  } else {
    try {
      console.error("Error(obj):", JSON.stringify(e));
    } catch {
      console.error("Error(unknown)");
    }
  }
}

try { throw { oops: true } } catch (e: unknown) { logErrorSafe(e) }

Tips

Tips:

  • Prefer unknown in catch.
  • Narrow with instanceof, typeof, or guards.
  • Avoid assuming Error—3rd‑party code may throw anything.

Error typing check

Quick check: Why prefer unknown in catch?

Recap

Recap: Catch as unknown, narrow with instanceof/typeof or custom guards, and centralize safe logging.

Frequently asked questions

Is the “Error typing (unknown), narrowing in catch” lesson free?

Yes — the full text of “Error typing (unknown), narrowing in catch” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Error typing (unknown), narrowing in catch”?

Type catch parameters as unknown, then narrow with instanceof/typeof or custom guards for safe error handling. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Error typing (unknown), narrowing in catch” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Promise , async/await typing
  2. Error typing (unknown), narrowing in catch
  3. Concurrency patterns (all/settled/race) types
← Back to TypeScript Academy