0Pricing
TypeScript Academy · Lesson

Option and Maybe Types

Model the presence or absence of a value explicitly.

Option and Maybe Types is a free TypeScript Academy lesson on CoddyKit — lesson 3 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, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Modeling Absence

Sometimes a value may simply be absent, with no error to report. An Option (also called Maybe) models presence or absence explicitly, an alternative to null and undefined.

Defining Option

Option is a discriminated union with a some tag. When some is true there is a value; when false there is nothing.

type Option<T> =
  | { some: true; value: T }
  | { some: false };

Constructors

Helpers some and none make Option values easy to create, mirroring ok/err from Result.

const some = <T>(value: T): Option<T> => ({ some: true, value });
const none: Option<never> = { some: false };

Returning an Option

A lookup that might find nothing returns an Option instead of T | undefined. The intent of optionality is explicit in the type.

function find<T>(arr: T[], pred: (x: T) => boolean): Option<T> {
  const hit = arr.find(pred);
  return hit === undefined ? none : some(hit);
}

Narrowing on some

Check opt.some to access the value safely. The compiler only allows reading value inside the present branch.

const r = find([1, 2, 3], n => n > 1);
if (r.some) console.log(r.value); // 2
else console.log("not found");

Why Not Just null?

null and undefined are easy to forget and blend into other types. An explicit Option forces the absence case to be handled and reads clearly as intentional.

Option vs T | undefined

Both can model absence, but Option is a deliberate, named structure you can attach helpers to (map, getOrElse), encouraging consistent handling across the codebase.

// undefined is implicit and easy to ignore;
// Option is explicit and self-documenting.

A getOrElse Helper

To collapse an Option to a concrete value, supply a fallback for the absent case. This keeps the absence handling in one obvious place.

function getOrElse<T>(o: Option<T>, fallback: T): T {
  return o.some ? o.value : fallback;
}
console.log(getOrElse(none, 0)); // 0

Mapping Over an Option

A map transforms the inner value only when present, leaving absence untouched. This lets you chain transformations without repeated checks.

function mapOpt<T, U>(o: Option<T>, f: (t: T) => U): Option<U> {
  return o.some ? some(f(o.value)) : none;
}
console.log(mapOpt(some(5), n => n * 2)); // { some: true, value: 10 }

Option vs Result

Use Option when absence carries no reason (a lookup miss). Use Result when failure has an explanatory error. Choose based on whether you need to convey why.

Composing Options

Because Option has map and getOrElse, chains of optional steps stay readable, with absence short-circuiting automatically through the chain.

const out = getOrElse(mapOpt(find([1,2], n => n > 1), n => n + 100), -1);
console.log(out); // 102

Quick Check

Quick check on this lesson.

Recap

Option<T> is { some: true; value } or { some: false }, modeling absence explicitly instead of null. Build with some/none, narrow on some, and use helpers like getOrElse and map. Prefer Option when absence needs no reason, Result when failure does.

Frequently asked questions

Is the “Option and Maybe Types” lesson free?

Yes — the full text of “Option and Maybe Types” is free to read here on the web, and the TypeScript Academy course includes 4 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 “Option and Maybe Types”?

Model the presence or absence of a value explicitly. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Option and Maybe Types” 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. The Problem with Throwing Errors
  2. Modeling Result Types
  3. Option and Maybe Types
  4. Railway-Oriented Programming
← Back to TypeScript Academy