1. Concept library
  2. JavaScript & TypeScript
  3. Module 11: Errors and validation

Concept

The unknown type

unknown is the safe any: a value of unknown type that you must check before using. It is the right type for parsed JSON, caught errors and any input from outside the program.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain the difference between unknown and any
  2. Narrow an unknown value before using it
  3. Type a caught error as unknown and extract its message safely

In practice

A few lines that show The unknown type doing its job.

TypeScript
function message(error: unknown): string {
  if (error instanceof Error) return error.message;
  if (typeof error === "string") return error;
  return "Unknown error";
}

try {
  JSON.parse("{bad json");
} catch (error) {              // error: unknown under useUnknownInCatchVariables
  console.error(message(error));
}

The common mistake

Watch for

Reaching for any because unknown "gets in the way". any switches checking off for everything it touches; unknown makes you narrow once, at the boundary, and the rest stays typed.

What to know first

Your roadmap puts these before The unknown type, unless you already know them.

How it stays learned

Once a session shows you understand The unknown type, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on The unknown type.

Previous in the module← Error handling patterns
Next in the moduleRuntime validation →