Skip to content

Option Type

In one line

Option<T> is Some<T> | None — explicit representation of optional values without null/undefined pitfalls.

In simple words

An Option type (also called Maybe) represents a value that might or might not exist. Instead of using null or undefined, you use Some(value) or None.

type Option<T> = { kind: "some"; value: T } | { kind: "none" }

Benefits:

  1. No null bugs — you can't forget to check for null because the type forces it
  2. Composable — map, filter, flatMap operations
  3. Explicit — the type signature shows a value might be absent
  4. No billion-dollar mistake — null reference errors are impossible

Inspired by Rust's Option, Haskell's Maybe. Combined with Result, this provides complete functional error handling without exceptions or null.

Example

typescript
type Option<T> = { kind: "some"; value: T } | { kind: "none" };

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

function findUser(id: string): Option<string> {
  if (id === "1") return some("Alice");
  return none();
}

const user = findUser("1");
if (user.kind === "some") {
  console.log(user.value); // "Alice"
} else {
  console.log("Not found");
}

// Can't access user.value without checking user.kind first!

Option forces you to check .kind before accessing .value. No null reference errors possible — absence is explicit in the type.

Common confusions

  • Confused with: nullable type

    The difference: A nullable type (T | null) allows null but doesn't force you to check for it before accessing properties. An Option type is a discriminated union that forces you to check .kind before accessing .value. Option is safer but more verbose; nullable is simpler but error-prone.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary