Typed error handling
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Narrow an unknown catch value safely.
- Define typed error classes and error codes.
- Choose between throw, Result and error callbacks.
Overview
#catch is unknown
try { risky(); }
catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.error(message);
}
Under useUnknownInCatchVariables you cannot read error.message without narrowing.
Typed errors
class AppError extends Error {
constructor(message: string, readonly code: string) {
super(message);
this.name = "AppError";
}
}
instanceof AppError narrows and gives access to code.
Error codes as a union
type ErrorCode = "not_found" | "timeout" | "unauthorized";
A union of codes is easier to switch on than free-form strings.
Choosing a strategy
- Throw for programming errors and invariants.
- Return a Result for expected failures the caller can handle.
- Use an error callback for streaming or event APIs where throwing is not possible.
Never swallow
An empty catch hides bugs. Log, rethrow or convert to a typed failure.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Implement `toMessage(error: unknown): string` returning the Error message or String(error).
starter.typescriptexport function toMessage(error: unknown): string { // your code }Reveal the reference answer
reference.typescriptexport function toMessage(error: unknown): string { return error instanceof Error ? error.message : String(error); }Authored expected output: Compiles with no errors. Runtime: toMessage(new Error("x")) is "x"; toMessage(1) is "1".
The instanceof check narrows the unknown value before reading message.
Exercise 2Predict
Is `error.message` allowed here under strict mode? ```ts try { risky(); } catch (error) { console.log(error.message); } ```
Exercise 3Explain
When would you return a Result instead of throwing, and when must you throw?
Reveal the reference answer
reference.typescriptReturn a Result when the failure is expected and the caller can act on it, such as a validation error or a not-found lookup. Throw when continuing would be meaningless, such as a missing required dependency or a broken invariant, because the caller cannot sensibly recover and the throw should reach a top-level handler.Authored expected output: A written answer separating expected failures from programming errors.
The choice follows from whether the caller can recover.
Common misconceptions
#- catch variables are typed any in strict mode.
- instanceof works across realms and bundled copies.
- Catching and ignoring is safe.
Follow-up practice
#- typescript-module-resolution
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.