effect-io-ai

Package: effect
Module: Option

Option.getOrThrowWith

Extracts the value of an Option or throws an error if the Option is None, using a custom error factory.

Details

This function allows you to extract the value of an Option when it is Some. If the Option is None, it throws an error generated by the provided onNone function. This utility is particularly useful when you need a fail-fast behavior for empty Option values and want to provide a custom error message or object.

Example

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

assert.deepStrictEqual(
  Option.getOrThrowWith(Option.some(1), () => new Error('Unexpected None')),
  1
)
assert.throws(() => Option.getOrThrowWith(Option.none(), () => new Error('Unexpected None')))

See

Signature

declare const getOrThrowWith: { (onNone: () => unknown): <A>(self: Option<A>) => A; <A>(self: Option<A>, onNone: () => unknown): A; }

Source

Since v2.0.0