FuncType - v0.60.3
    Preparing search index...

    Interface Option<T>

    The Option type represents a value that may or may not exist. It's used to handle potentially null or undefined values in a type-safe way.

    interface Option<out T extends Type> {
        _tag: "Some" | "None";
        "[toStringTag]": string;
        isEmpty: boolean;
        size: number;
        value: T | undefined;
        ap<U extends unknown>(ff: Option<(value: T) => U>): Option<U>;
        contains(value: T): boolean;
        count(p: (x: T) => boolean): number;
        doUnwrap(): DoResult<T>;
        exists(p: (a: T) => boolean): boolean;
        filter(predicate: (value: T) => boolean): Option<T>;
        find(p: (a: T) => boolean): Option<T>;
        flatMap<U extends unknown>(f: (value: T) => Option<U>): Option<U>;
        flatMapAsync<U extends unknown>(
            f: (value: T) => Promise<Option<U>>,
        ): Promise<Option<U>>;
        fold<U>(onNone: () => U, onSome: (value: T) => U): U;
        foldAsync<U>(
            onNone: () => U | Promise<U>,
            onSome: (value: T) => U | Promise<U>,
        ): Promise<U>;
        foldLeft<B>(z: B): (op: (b: B, a: T) => B) => B;
        foldRight<B>(z: B): (op: (a: T, b: B) => B) => B;
        forEach(f: (a: T) => void): void;
        isNone(): this is Option<T> & { isEmpty: true; value: undefined };
        isSome(): this is Option<T> & { isEmpty: false; value: T };
        map<U extends unknown>(f: (value: T) => U): Option<U>;
        match<R>(patterns: { None: () => R; Some: (value: T) => R }): R;
        or<T2 extends unknown>(alternative: Option<T2>): Option<T | T2>;
        orElse<T2 extends unknown>(defaultValue: T2): T | T2;
        orNull(): T | null;
        orThrow(error?: Error): T;
        orUndefined(): T | undefined;
        pipe<U extends unknown>(f: (value: T) => U): U;
        reduce<B = T>(
            op: (b: Widen<T, B>, a: Widen<T, B>) => Widen<T, B>,
        ): Widen<T, B>;
        reduceRight<B = T>(
            op: (b: Widen<T, B>, a: Widen<T, B>) => Widen<T, B>,
        ): Widen<T, B>;
        serialize(): SerializationMethods<T>;
        toEither<E>(left: E): Either<E, T>;
        toList(): List<T>;
        toOption(): Option<T>;
        toPromise(): Promise<T>;
        toString(): string;
        toTry(): Try<T>;
        toValue(): { _tag: "Some" | "None"; value: T };
    }

    Type Parameters

    • out T extends Type

      The type of the value contained in the Option

    Hierarchy (View Summary)

    Index

    Properties

    _tag: "Some" | "None"
    "[toStringTag]": string
    isEmpty: boolean

    Whether this Option contains no value

    size: number

    The number of elements in this Option (0 or 1)

    value: T | undefined

    The contained value (undefined for None)

    Methods

    • Applies a wrapped function to a wrapped value (Applicative pattern)

      Type Parameters

      • U extends unknown

      Parameters

      • ff: Option<(value: T) => U>

        An Option containing a function from T to U

      Returns Option<U>

      A new Option containing the result of applying the function

    • Checks if this Option contains the specified value

      Parameters

      • value: T

        The value to check for

      Returns boolean

      true if this Option contains the value, false otherwise

    • Counts elements that satisfy the predicate. For single-value containers: returns 0 or 1 For collections: returns the count of matching elements

      Parameters

      • p: (x: T) => boolean

      Returns number

    • Tests whether any element satisfies the predicate. For single-value containers: tests the single value For collections: returns true if any element matches

      Parameters

      • p: (a: T) => boolean

      Returns boolean

    • Returns this Option if it contains a value that satisfies the predicate, otherwise returns None

      Parameters

      • predicate: (value: T) => boolean

        The predicate function to test the value

      Returns Option<T>

      This Option or None

    • Finds the first element that satisfies the predicate. For single-value containers: returns Some(value) if predicate matches, None otherwise For collections: returns the first matching element wrapped in Option

      Parameters

      • p: (a: T) => boolean

      Returns Option<T>

    • Maps the value using a function that returns an Option

      Type Parameters

      • U extends unknown

      Parameters

      • f: (value: T) => Option<U>

        The mapping function returning an Option

      Returns Option<U>

      The result of applying f to the contained value, or None if this Option is None

    • Maps the value using an async function that returns an Option

      Type Parameters

      • U extends unknown

      Parameters

      • f: (value: T) => Promise<Option<U>>

        The async mapping function returning an Option

      Returns Promise<Option<U>>

      Promise of the result of applying f to the contained value, or None if this Option is None

    • Pattern matches over the Option, applying onNone if None and onSome if Some

      Type Parameters

      • U

      Parameters

      • onNone: () => U

        Function to apply if the Option is None

      • onSome: (value: T) => U

        Function to apply if the Option has a value

      Returns U

      The result of applying the appropriate function

    • Async variant of fold. Accepts sync or async handlers on either branch and always returns a Promise.

      Type Parameters

      • U

      Parameters

      • onNone: () => U | Promise<U>
      • onSome: (value: T) => U | Promise<U>

      Returns Promise<U>

    • Left-associative fold using the provided zero value and operation

      Type Parameters

      • B

      Parameters

      • z: B

        Zero/identity value

      Returns (op: (b: B, a: T) => B) => B

      A function that takes an operation to apply

    • Right-associative fold using the provided zero value and operation

      Type Parameters

      • B

      Parameters

      • z: B

        Zero/identity value

      Returns (op: (a: T, b: B) => B) => B

      A function that takes an operation to apply

    • Applies an effect function to each element. For single-value containers: applies to the value if present For collections: applies to each element

      Parameters

      • f: (a: T) => void

      Returns void

    • Returns true if this Option is a None (contains no value)

      Returns this is Option<T> & { isEmpty: true; value: undefined }

      true if this Option is empty, false otherwise

    • Returns true if this Option is a Some (contains a value)

      Returns this is Option<T> & { isEmpty: false; value: T }

      true if this Option contains a value, false otherwise

    • Maps the value inside the Option using the provided function

      Type Parameters

      • U extends unknown

      Parameters

      • f: (value: T) => U

        The mapping function

      Returns Option<U>

      A new Option containing the mapped value, or None if this Option is None

    • Pattern matches over the Option, applying a handler function based on the variant

      Type Parameters

      • R

      Parameters

      • patterns: { None: () => R; Some: (value: T) => R }

        Object with handler functions for Some and None variants

      Returns R

      The result of applying the matching handler function

    • Returns this Option if it contains a value, otherwise returns the alternative container. The alternative may hold a different type; the result widens to Option<T | T2>.

      Type Parameters

      • T2 extends unknown

      Parameters

      • alternative: Option<T2>

        The alternative Option to return if this is None

      Returns Option<T | T2>

      This Option or the alternative, typed as Option<T | T2>

    • Returns the contained value or a default value if None. The default may be of a different type; the result widens to T | T2 so Option stays covariant in T.

      Type Parameters

      • T2 extends unknown

      Parameters

      • defaultValue: T2

        The value to return if this Option is None

      Returns T | T2

      The contained value or defaultValue, typed as T | T2

    • Returns the contained value or null if None

      Returns T | null

      The contained value or null

    • Returns the contained value or throws an error if None

      Parameters

      • Optionalerror: Error

        Optional custom error to throw. If not provided, throws a default error

      Returns T

      The contained value

      The specified error or a default error if the Option is None

    • Returns the contained value or undefined if None

      Returns T | undefined

      The contained value or undefined

    • Pipes the value through the provided function

      Type Parameters

      • U extends unknown

        The return type of the function

      Parameters

      • f: (value: T) => U

        The function to apply to the value

      Returns U

      The result of applying the function to the value

    • Converts this Option to an Either

      Type Parameters

      • E

      Parameters

      • left: E

        The value to use for Left if this Option is None

      Returns Either<E, T>

      Either.Right with the contained value if Some, or Either.Left with left if None

    • Converts this Option to a List.

      Returns List<T>

      A List containing the value if Some, or empty List if None

    • Converts this monad to an Option.

      Conversion rules:

      • Option: returns self
      • Either: Right → Some, Left → None
      • List: non-empty → Some(head), empty → None
      • Try: Success → Some, Failure → None

      Returns Option<T>

      An Option containing the value if present, None otherwise

    • Converts this container to a Promise

      The behavior depends on the implementing container:

      • Success/Right/Some containers resolve with their value
      • Failure/Left/None containers reject with their error/default error

      Returns Promise<T>

      A Promise that resolves or rejects based on the container's state

    • Returns a string representation of this Option

      Returns string

      A string representation

    • Converts this monad to a Try.

      Conversion rules:

      • Option: Some → Success, None → Failure(Error("None"))
      • Either: Right → Success, Left → Failure(Error(leftValue))
      • List: non-empty → Success(head), empty → Failure(Error("Empty list"))
      • Try: returns self

      Returns Try<T>

      A Try containing Success with the value or Failure with an appropriate error

    • Returns a simple object representation of this Option

      Returns { _tag: "Some" | "None"; value: T }

      An object with _tag and value properties