Hyperlinkv0.8.0-beta.28

Option

Option.flatMapNullishOrconsteffect/Option.ts:1404
<A, B>(f: (a: A) => B): (self: Option<A>) => Option<NonNullable<B>>
<A, B>(self: Option<A>, f: (a: A) => B): Option<NonNullable<B>>

Combines flatMap with fromNullishOr: applies a function that may return null/undefined to the value of a Some.

When to use

Use when you need to chain optional computations that use null or undefined instead of Option, such as nested property access.

Details

Example (Navigating optional properties)

import { Option } from "effect"

interface Employee {
  company?: { address?: { street?: { name?: string } } }
}

const emp: Employee = {
  company: { address: { street: { name: "high street" } } }
}

console.log(
  Option.some(emp).pipe(
    Option.flatMapNullishOr((e) => e.company?.address?.street?.name)
  )
)
// Output: { _id: 'Option', _tag: 'Some', value: 'high street' }
Source effect/Option.ts:14048 lines
export const flatMapNullishOr: {
  <A, B>(f: (a: A) => B): (self: Option<A>) => Option<NonNullable<B>>
  <A, B>(self: Option<A>, f: (a: A) => B): Option<NonNullable<B>>
} = dual(
  2,
  <A, B>(self: Option<A>, f: (a: A) => B): Option<NonNullable<B>> =>
    isNone(self) ? none() : fromNullishOr(f(self.value))
)