Hyperlinkv0.8.0-beta.28

Option

Option.fromUndefinedOrconsteffect/Option.ts:860
<A>(a: A): Option<Exclude<A, undefined>>

Converts a possibly undefined value into an Option, leaving null as a valid Some.

When to use

Use when you want to treat only undefined as absent while preserving null as a meaningful value.

Details

  • undefinedNone
  • Any other value (including null) → Some

Example (Converting possibly undefined values to an Option)

import { Option } from "effect"

console.log(Option.fromUndefinedOr(undefined))
// Output: { _id: 'Option', _tag: 'None' }

console.log(Option.fromUndefinedOr(null))
// Output: { _id: 'Option', _tag: 'Some', value: null }

console.log(Option.fromUndefinedOr(42))
// Output: { _id: 'Option', _tag: 'Some', value: 42 }
Source effect/Option.ts:8603 lines
export const fromUndefinedOr = <A>(
  a: A
): Option<Exclude<A, undefined>> => (a === undefined ? none() : some(a as Exclude<A, undefined>))
Referenced by 7 symbols