Hyperlinkv0.8.0-beta.28

Option

Option.getOrThrowWithconsteffect/Option.ts:1084
(onNone: () => unknown): <A>(self: Option<A>) => A
<A>(self: Option<A>, onNone: () => unknown): A

Extracts the value from a Some, or throws a custom error for None.

When to use

Use when you need fail-fast unwrapping of an Option for unexpected absence and want to provide a descriptive debugging error.

Details

  • Some → returns the inner value
  • None → throws the value returned by onNone()

Example (Throwing a custom error)

import { Option } from "effect"

console.log(Option.getOrThrowWith(Option.some(1), () => new Error("missing")))
// Output: 1

Option.getOrThrowWith(Option.none(), () => new Error("missing"))
// throws Error: missing
Source effect/Option.ts:10849 lines
export const getOrThrowWith: {
  (onNone: () => unknown): <A>(self: Option<A>) => A
  <A>(self: Option<A>, onNone: () => unknown): A
} = dual(2, <A>(self: Option<A>, onNone: () => unknown): A => {
  if (isSome(self)) {
    return self.value
  }
  throw onNone()
})
Referenced by 4 symbols