Option

Handle nullable values safely without null/undefined errors

What is Option?

Option is your first step into functional programming. It elegantly handles values that might be absent, eliminating null pointer exceptions and making your code more robust. Instead of using null or undefined, which can cause runtime errors, Option forces you to explicitly handle both cases at compile time.

// Instead of this:
const user = getUser() // might be null!

// Use this:
const user: Option<User> = getUser()
Some & None
fromPredicate
fold
getOrElse
filter
🛡️

Safe Null Handling

Never worry about null or undefined runtime errors again

🔗

Composable Operations

Chain operations together without nested if-statements

Type Safety

Compiler-enforced handling of all possible cases

Functional Style

Pure functions with predictable behavior

Option in Action

❌ Traditional Approach

function getUser(id: number): User | null {
  return users.find(u => u.id === id) ?? null
}

const user = getUser(1)
if (user !== null) {
  console.log(user.name) // Might still crash!
} else {
  console.log('User not found')
}

✅ Option Approach

function getUser(id: number): Option<User> {
  const user = users.find(u => u.id === id)
  return user ? O.some(user) : O.none
}

const result = pipe(
  getUser(1),
  O.fold(
    () => 'User not found',
    user => user.name
  )
) // Type safe and elegant!

Practice Exercises

1

Some And None

Learn about some and none in fp-ts option

beginnerStart
2

Of

Learn about of in fp-ts option

beginnerStart
3

From Predicate

Learn about from predicate in fp-ts option

beginnerStart
4

Fold

Learn about fold in fp-ts option

intermediateStart
5

From Nullable

Learn about from nullable in fp-ts option

intermediateStart
6

To Nullable

Learn about to nullable in fp-ts option

intermediateStart
7

To Undefined

Learn about to undefined in fp-ts option

advancedStart
8

Get Or Else

Learn about get or else in fp-ts option

advancedStart
9

Filter

Learn about filter in fp-ts option

advancedStart
10

From Either

Learn about from either in fp-ts option

advancedStart

Why Learn Option?

🛡️

Master Option

Learn the fundamental concepts and patterns that make Option powerful

💪

10 Exercises

Practice with hands-on exercises from beginner friendly level

🚀

Production Ready

Apply Option patterns to build robust, type-safe applications