<A>(a: A): Option<Exclude<A, null>>Converts a possibly null value into an Option, leaving undefined
as a valid Some.
When to use
Use when you want to treat only null as absent while preserving
undefined as a meaningful value.
Details
null→None- Any other value (including
undefined) →Some
Example (Converting possibly null values to an Option)
import { Option } from "effect"
console.log(Option.fromNullOr(null))
// Output: { _id: 'Option', _tag: 'None' }
console.log(Option.fromNullOr(undefined))
// Output: { _id: 'Option', _tag: 'Some', value: undefined }
console.log(Option.fromNullOr(42))
// Output: { _id: 'Option', _tag: 'Some', value: 42 }export const const fromNullOr: <A>(
a: A
) => Option<Exclude<A, null>>
Converts a possibly null value into an Option, leaving undefined
as a valid Some.
When to use
Use when you want to treat only null as absent while preserving
undefined as a meaningful value.
Details
null → None
- Any other value (including
undefined) → Some
Example (Converting possibly null values to an Option)
import { Option } from "effect"
console.log(Option.fromNullOr(null))
// Output: { _id: 'Option', _tag: 'None' }
console.log(Option.fromNullOr(undefined))
// Output: { _id: 'Option', _tag: 'Some', value: undefined }
console.log(Option.fromNullOr(42))
// Output: { _id: 'Option', _tag: 'Some', value: 42 }
fromNullOr = <function (type parameter) A in <A>(a: A): Option<Exclude<A, null>>A>(
a: Aa: function (type parameter) A in <A>(a: A): Option<Exclude<A, null>>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 Exclude<T, U> = T extends U
? never
: T
Exclude from T those types that are assignable to U
Exclude<function (type parameter) A in <A>(a: A): Option<Exclude<A, null>>A, null>> => (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: A & ({} | undefined)a as type Exclude<T, U> = T extends U
? never
: T
Exclude from T those types that are assignable to U
Exclude<function (type parameter) A in <A>(a: A): Option<Exclude<A, null>>A, null>))