<A extends ReadonlyArray<unknown>, B>(f: (...a: A) => B): (
...a: A
) => Option<B>Lifts a function that may throw into one that returns an Option.
When to use
Use to wrap exception-throwing APIs (e.g. JSON.parse) for safe usage
Details
- If the function returns normally →
Somewith the result - If the function throws →
None(exception is swallowed)
Example (Lifting JSON.parse)
import { Option } from "effect"
const parse = Option.liftThrowable(JSON.parse)
console.log(parse("1"))
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
console.log(parse(""))
// Output: { _id: 'Option', _tag: 'None' }export const const liftThrowable: <
A extends ReadonlyArray<unknown>,
B
>(
f: (...a: A) => B
) => (...a: A) => Option<B>
Lifts a function that may throw into one that returns an Option.
When to use
Use to wrap exception-throwing APIs (e.g. JSON.parse) for safe usage
Details
- If the function returns normally →
Some with the result
- If the function throws →
None (exception is swallowed)
Example (Lifting JSON.parse)
import { Option } from "effect"
const parse = Option.liftThrowable(JSON.parse)
console.log(parse("1"))
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
console.log(parse(""))
// Output: { _id: 'Option', _tag: 'None' }
liftThrowable = <function (type parameter) A in <A extends ReadonlyArray<unknown>, B>(f: (...a: A) => B): (...a: A) => Option<B>A extends interface ReadonlyArray<T>ReadonlyArray<unknown>, function (type parameter) B in <A extends ReadonlyArray<unknown>, B>(f: (...a: A) => B): (...a: A) => Option<B>B>(
f: (...a: A) => Bf: (...a: A extends ReadonlyArray<unknown>a: function (type parameter) A in <A extends ReadonlyArray<unknown>, B>(f: (...a: A) => B): (...a: A) => Option<B>A) => function (type parameter) B in <A extends ReadonlyArray<unknown>, B>(f: (...a: A) => B): (...a: A) => Option<B>B
): (...a: A extends ReadonlyArray<unknown>a: function (type parameter) A in <A extends ReadonlyArray<unknown>, B>(f: (...a: A) => B): (...a: A) => Option<B>A) => type Option<A> = None<A> | Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) B in <A extends ReadonlyArray<unknown>, B>(f: (...a: A) => B): (...a: A) => Option<B>B> =>
(...a: A extends ReadonlyArray<unknown>a) => {
try {
return const some: <A>(value: A) => Option<A>Wraps the given value into an Option to represent its presence.
When to use
Use to wrap a known present value as Option
- Returning a successful result from a partial function
Details
- Always returns
Some<A>
- Does not filter
null or undefined; use
fromNullishOr
for that
Example (Wrapping a value)
import { Option } from "effect"
// ┌─── Option<number>
// ▼
const value = Option.some(1)
console.log(value)
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
some(f: (...a: A) => Bf(...a: A extends ReadonlyArray<unknown>a))
} catch {
return const none: <A = never>() => Option<A>Creates an Option representing the absence of a value.
When to use
Use to represent a missing or uninitialized value, such as returning "no
result" from a function.
Details
- Returns
Option<never>, which is a subtype of Option<A> for any A
- Always returns the same singleton instance
Example (Creating an empty Option)
import { Option } from "effect"
// ┌─── Option<never>
// ▼
const noValue = Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
none()
}
}