The Result Pattern: Ok and Err
Return errors as values instead of throwing.
The Result Pattern: Ok and Err is a free TypeScript Academy lesson on CoddyKit. This is lesson 2 of 4. 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, and your progress syncs across the web and the CoddyKit app. The TypeScript Academy course includes 4 lessons in total.
Welcome
Result Type
type Ok<T> = { ok: true; value: T };
type Err<E = Error> = { ok: false; error: E };
type Result<T, E = Error> = Ok<T> | Err<E>;Helper Constructors
const ok = <T>(value: T): Ok<T> => ({ ok: true, value });
const err = <E>(error: E): Err<E> => ({ ok: false, error });Returning Results
async function fetchUser(id: number): Promise<Result<User>> {
try {
const user = await db.findUser(id);
if (!user) return err(new Error('Not found'));
return ok(user);
} catch (e) { return err(e as Error); }
}Consuming Results
const result = await fetchUser(1);
if (result.ok) {
console.log(result.value.name); // User
} else {
console.error(result.error.message);
}mapResult Helper
function mapResult<T, U, E>(r: Result<T, E>, fn: (v: T) => U): Result<U, E> {
return r.ok ? ok(fn(r.value)) : r;
}flatMapResult Helper
function flatMap<T, U, E>(r: Result<T, E>, fn: (v: T) => Result<U, E>): Result<U, E> {
return r.ok ? fn(r.value) : r;
}Result vs Exceptions
Library Support
Result in API Handlers
app.get('/users/:id', async (req, res) => {
const result = await fetchUser(parseInt(req.params.id));
if (result.ok) res.json(result.value);
else res.status(404).json({ error: result.error.message });
});Async Results
type AsyncResult<T, E = Error> = Promise<Result<T, E>>;Quick Check
Recap
Learn TypeScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 101
- Lessons
- 352
Frequently Asked Questions
Is the “The Result Pattern: Ok and Err” lesson free?
Yes — the full text of “The Result Pattern: Ok and Err” is free to read here on the web. 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. The TypeScript Academy course includes 4 lessons in total.
What will I learn in “The Result Pattern: Ok and Err”?
Return errors as values instead of throwing. 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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “The Result Pattern: Ok and Err” 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
- Typed Error Classes and Hierarchies
- The Result Pattern: Ok and Err
- Narrowing Caught Errors (unknown vs Error)
- Error Handling in Async TypeScript Code