@dbidwell94/ts-utils
    Preparing search index...

    Interface OptionUtils<T>

    interface OptionUtils<T> {
        andThen<NewT>(mapFn: (option: T) => Option<NewT>): Option<NewT>;
        expect(message: string): T;
        inspect(callback: (value: T) => void): Option<T>;
        isNone(): this is None;
        isSome(): this is Some<T>;
        map<NewT>(mapFn: (option: T) => NewT): Option<NewT>;
        okOr<E extends Error>(error: E): Result<T, E>;
        okOr<E extends Error>(error: string): Result<T, Error>;
        okOr<E extends Error>(error: string | Error): Result<T, E>;
        serialize(): SerializableOption<T>;
        unsafeUnwrap(): undefined | null | T;
        unsafeUnwrapOr(defaultValue?: null | T): undefined | null | T;
        unwrap(): T;
        unwrapOr(defaultValue: T): T;
    }

    Type Parameters

    • T
    Index

    Methods

    • maps the value of this Option to a Option using the provided function. This is useful if you wish to chain multiple Option types together. Flattens the Option<Option> to an Option

      Type Parameters

      • NewT

      Parameters

      • mapFn: (option: T) => Option<NewT>

        The function which will be called if the Option is a Some

      Returns Option<NewT>

    • Extracts the base value from this type. The function throws an error with the provided message if there is no value provided.

      Parameters

      • message: string

        The message to throw if the Option<T> is a None.

      Returns T

      The value of the Option<T> if it is a Some<T>.

      if the default value is null or undefined.

    • Peek into the inner value of this Option, and if isSome() then the callback function will be run.

      Parameters

      • callback: (value: T) => void

        the callback to run if this Option isSome

      Returns Option<T>

      this Option<T>

    • Utility function to determine whether this Option<T> type is a None type.

      Returns this is None

      true if the Option<T> is a None, otherwise false.

    • Utility function to determine whether this Option<T> type is a Some type.

      Returns this is Some<T>

      true if the Option<T> is a Some<T>, otherwise false.

    • Maps the inner value of the Option to a new value, returning a completely new Option

      Type Parameters

      • NewT

      Parameters

      • mapFn: (option: T) => NewT

        The function which will be called if the Option is a Some

      Returns Option<NewT>

      A new Option with the mapped value

    • Converts this Option<T> type into a Result<T, E> type. The Result<T, E> will be an Err<E> if the Option<T> is a None.

      Type Parameters

      • E extends Error

      Parameters

      • error: E

        The error to return if the Option<T> is a None.

      Returns Result<T, E>

      A Result<T, E> type.

    • Type Parameters

      • E extends Error

      Parameters

      • error: string

      Returns Result<T, Error>

    • Type Parameters

      • E extends Error

      Parameters

      • error: string | Error

      Returns Result<T, E>

    • A utility function to convert an Option to a SerializableOption. This is a useful function if you wish to store an Option in Redux, as Redux does not allow non-serializable fields (functions) in its store.

      Returns SerializableOption<T>

      An Option without any helper functions

    • Extracts the base value from this type, not checking if the value is None.

      Returns undefined | null | T

      The raw unchecked inner value of the Option<T>

    • Extracts the base value from this type. If there is no value in the Option<T>, the function will return whatever you provided as the default value, even if that value was not provided (undefined).

      Parameters

      • OptionaldefaultValue: null | T

        The value to return if the Option<T> is a None

      Returns undefined | null | T

      The possibly undefined | null value of the Option<T>.

    • Extracts the base value from this type. This function will throw an error if there is no value in the Option<T>.

      Returns T

      The value of the Option<T>.

      if the Option<T> is a None.

    • Extracts the base value from this type. If there is no value in the Option<T>, the function will return the provided default value.

      Parameters

      • defaultValue: T

        The value to return if the Option<T> is a None.

      Returns T

      The value of the Option<T> if it is a Some<T>, otherwise the default value.

      if the default value is null or undefined.