Hyperlinkv0.8.0-beta.28

Option

Option.bindconsteffect/Option.ts:2440
<N extends string, A extends object, B>(
  name: Exclude<N, keyof A>,
  f: (a: NoInfer<A>) => Option<B>
): (
  self: Option<A>
) => Option<{ [K in N | keyof A]: K extends keyof A ? A[K] : B }>
<A extends object, N extends string, B>(
  self: Option<A>,
  name: Exclude<N, keyof A>,
  f: (a: NoInfer<A>) => Option<B>
): Option<{ [K in N | keyof A]: K extends keyof A ? A[K] : B }>

Adds an Option value to the do notation record under a given name. If the Option is None, the whole pipeline short-circuits to None.

When to use

Use when you need to sequence Option computations in do notation.

Example (Binding Option values)

import { Option, pipe } from "effect"
import * as assert from "node:assert"

const result = pipe(
  Option.Do,
  Option.bind("x", () => Option.some(2)),
  Option.bind("y", () => Option.some(3)),
  Option.let("sum", ({ x, y }) => x + y),
  Option.filter(({ x, y }) => x * y > 5)
)
assert.deepStrictEqual(result, Option.some({ x: 2, y: 3, sum: 5 }))
do notationDolet_bindTo
Source effect/Option.ts:244011 lines
export const bind: {
  <N extends string, A extends object, B>(
    name: Exclude<N, keyof A>,
    f: (a: NoInfer<A>) => Option<B>
  ): (self: Option<A>) => Option<{ [K in N | keyof A]: K extends keyof A ? A[K] : B }>
  <A extends object, N extends string, B>(
    self: Option<A>,
    name: Exclude<N, keyof A>,
    f: (a: NoInfer<A>) => Option<B>
  ): Option<{ [K in N | keyof A]: K extends keyof A ? A[K] : B }>
} = doNotation.bind<OptionTypeLambda>(map, flatMap)