<A>(a: A): Option<NonNullable<A>>Converts a nullable value (null or undefined) into an Option.
When to use
Use when you need JavaScript nullish values to become absence at an API boundary while all other values, including falsy ones, remain present.
Details
nullorundefined→None- Any other value →
Some(typed asNonNullable<A>)
Example (Converting nullable values to an Option)
import { Option } from "effect"
console.log(Option.fromNullishOr(undefined))
// Output: { _id: 'Option', _tag: 'None' }
console.log(Option.fromNullishOr(null))
// Output: { _id: 'Option', _tag: 'None' }
console.log(Option.fromNullishOr(1))
// Output: { _id: 'Option', _tag: 'Some', value: 1 }export const const fromNullishOr: <A>(
a: A
) => Option<NonNullable<A>>
Converts a nullable value (null or undefined) into an Option.
When to use
Use when you need JavaScript nullish values to become absence at an API
boundary while all other values, including falsy ones, remain present.
Details
null or undefined → None
- Any other value →
Some (typed as NonNullable<A>)
Example (Converting nullable values to an Option)
import { Option } from "effect"
console.log(Option.fromNullishOr(undefined))
// Output: { _id: 'Option', _tag: 'None' }
console.log(Option.fromNullishOr(null))
// Output: { _id: 'Option', _tag: 'None' }
console.log(Option.fromNullishOr(1))
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
fromNullishOr = <function (type parameter) A in <A>(a: A): Option<NonNullable<A>>A>(
a: Aa: function (type parameter) A in <A>(a: A): Option<NonNullable<A>>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<type NonNullable<T> = T & {}Exclude null and undefined from T
NonNullable<function (type parameter) A in <A>(a: A): Option<NonNullable<A>>A>> => (a: Aa == null ? 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() : 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(a: NonNullable<A>a as type NonNullable<T> = T & {}Exclude null and undefined from T
NonNullable<function (type parameter) A in <A>(a: A): Option<NonNullable<A>>A>))