effect-io-ai

Package: effect
Module: Option

Option.liftNullable

Lifts a function that returns null or undefined into the Option context.

Details

This function takes a function f that might return null or undefined and transforms it into a function that returns an Option. The resulting function will return:

Example

import { Option } from "effect"

const parse = (s: string): number | undefined => {
  const n = parseFloat(s)
  return isNaN(n) ? undefined : n
}

const parseOption = Option.liftNullable(parse)

console.log(parseOption("1"))
// Output: { _id: 'Option', _tag: 'Some', value: 1 }

console.log(parseOption("not a number"))
// Output: { _id: 'Option', _tag: 'None' }

Signature

declare const liftNullable: <A extends ReadonlyArray<unknown>, B>(f: (...a: A) => B | null | undefined) => (...a: A) => Option<NonNullable<B>>

Source

Since v2.0.0