Effect.ts: Absence as First-Class

Data TypesLesson 27 of 51

27. Option Composition

Transform absent values without null checks!

Code Example
const user = Option.some({ age: 25 });

// Chain transformations on potentially absent values
const canDrink = user.pipe(
  Option.map(u => u.age),         // Option<number>
  Option.filter(age => age >= 21), // Filter out if < 21
  Option.map(age => true)          // Option<boolean>
);

// Option.match handles both cases
const result = canDrink.pipe(
  Option.match({
    onNone: () => "Cannot drink",
    onSome: () => "Can drink"
  })
);

// No null checks!
// Absence flows through the pipeline!
Interactive Example
const { Option } = await import('effect');
const user = Option.some({ age: 25 });
const canDrink = user.pipe(
      Option.map(u => u.age),
      Option.filter(age => age >= 21),
      Option.map(() => true)
);
const result = Option.match(canDrink, {
      onNone: () => 'Cannot drink',
      onSome: () => 'Can drink'
});
return `Option composition!User age: 25Filter >=21: PassResult: ${result}No null checks!`;
Explanation

Option composition: transforming absence without assumptions.

Traditional approach:

const age = user?.age;
if (age && age >= 21) { ... }  // Nested checks!

Option approach:

  • Transform absent values like present ones
  • Filter propagates absence
  • Map propagates absence
  • No assumptions at each step

Absence-first transformation: null checks are in the types, not your code!


Part 27 of 51 in the Effect.ts Absence Modeling series