Hyperlinkv0.8.0-beta.28

Option

Option.allconsteffect/Option.ts:1735
<const I extends Iterable<Option<any>> | Record<string, Option<any>>>(
  input: I
): [I] extends [ReadonlyArray<Option<any>>]
  ? Option<{
      -readonly [K in keyof I]: [I[K]] extends [Option<infer A>]
        ? A
        : never
    }>
  : [I] extends [Iterable<Option<infer A>>]
  ? Option<Array<A>>
  : Option<{
      -readonly [K in keyof I]: [I[K]] extends [Option<infer A>]
        ? A
        : never
    }>

Combines a structure of Options (tuple, struct, or iterable) into a single Option containing the unwrapped structure.

When to use

Use when you need to combine multiple Option values into one while preserving the input shape, with any None making the result None.

Details

  • Tuple input → Option of a tuple with the same length
  • Struct input → Option of a struct with the same keys
  • Iterable input → Option of an Array
  • Any None in the input → entire result is None

Example (Combining a tuple and a struct)

import { Option } from "effect"

const maybeName: Option.Option<string> = Option.some("John")
const maybeAge: Option.Option<number> = Option.some(25)

//      ┌─── Option<[string, number]>
//      ▼
const tuple = Option.all([maybeName, maybeAge])
console.log(tuple)
// Output:
// { _id: 'Option', _tag: 'Some', value: [ 'John', 25 ] }

//      ┌─── Option<{ name: string; age: number; }>
//      ▼
const struct = Option.all({ name: maybeName, age: maybeAge })
console.log(struct)
// Output:
// { _id: 'Option', _tag: 'Some', value: { name: 'John', age: 25 } }
Source effect/Option.ts:173530 lines
export const all: <const I extends Iterable<Option<any>> | Record<string, Option<any>>>(
  input: I
) => [I] extends [ReadonlyArray<Option<any>>] ? Option<
    { -readonly [K in keyof I]: [I[K]] extends [Option<infer A>] ? A : never }
  >
  : [I] extends [Iterable<Option<infer A>>] ? Option<Array<A>>
  : Option<{ -readonly [K in keyof I]: [I[K]] extends [Option<infer A>] ? A : never }> = (
    input: Iterable<Option<any>> | Record<string, Option<any>>
  ): Option<any> => {
    if (Symbol.iterator in input) {
      const out: Array<Option<any>> = []
      for (const o of (input as Iterable<Option<any>>)) {
        if (isNone(o)) {
          return none()
        }
        out.push(o.value)
      }
      return some(out)
    }

    const out: Record<string, any> = {}
    for (const key of Object.keys(input)) {
      const o = input[key]
      if (isNone(o)) {
        return none()
      }
      out[key] = o.value
    }
    return some(out)
  }