Variable Option

Option: {
    andThen: (<T, U>(mapper: Mapper<T, Option<U>>) => Mapper<Option<T>, Option<U>>);
    from: (<T>(value: T) => Option<T>);
    isInstance: (<T>(value: unknown) => value is Option<T>);
    isNone: (<T>() => Mapper<Option<T>, boolean>);
    isSome: (<T>() => Mapper<Option<T>, boolean>);
    map: (<T, U>(mapper: Mapper<T, U>) => Mapper<Option<T>, Option<U>>);
    none: (<T>() => Option<T>);
    okOr: (<T, E>(error: E | Producer<E>) => Mapper<Option<T>, Result<T, E>>);
    okOrError: (<T>(message: string | Producer<string>) => Mapper<Option<T>, Result<T, Error>>);
    transpose: (<T, E>(option: Option<Result<T, E>>) => Result<Option<T>, E>);
    valueOr: (<T>(fallback: T | Producer<T>) => Mapper<Option<T>, T>);
}

Option-related utilities.

Type declaration

  • andThen: (<T, U>(mapper: Mapper<T, Option<U>>) => Mapper<Option<T>, Option<U>>)

    Higher order function returning a function that performs the andThen method on the provided Option.

    The returned function chains a new Option to the current one if it is Some.

  • from: (<T>(value: T) => Option<T>)

    Creates a new Option from a nullable, potentially-undefined value.

    const somthing = Option.from(123);               // Some(123)
    const nothingNull = Option.from(null); // None
    const nothingUndefined = Option.from(undefined); // None
      • <T>(value): Option<T>
      • Type Parameters

        • T

        Parameters

        • value: T

          The value to wrap in an Option.

        Returns Option<T>

        A new Option with the left-value Some if the value is not null or undefined, otherwise None.

  • isInstance: (<T>(value: unknown) => value is Option<T>)

    Checks if a value is an Option jonad.

    Option.isInstance(Option.from(123)); // true
    
    if (Option.isInstance<number>(value)) {
    // value is now Option<number>
    return value.valueOr(0);
      • <T>(value): value is Option<T>
      • Type Parameters

        • T

        Parameters

        • value: unknown

          The value to check.

        Returns value is Option<T>

        true if the value is an Option, false otherwise

  • isNone: (<T>() => Mapper<Option<T>, boolean>)

    Higher order function returning a function that performs the isNone method on the provided Option.

    The returned function checks if the value is None.

  • isSome: (<T>() => Mapper<Option<T>, boolean>)

    Higher order function returning a function that performs the isSome method on the provided Option.

    The returned function checks if the value is Some.

  • map: (<T, U>(mapper: Mapper<T, U>) => Mapper<Option<T>, Option<U>>)

    Higher order function returning a function that performs the map method on the provided Option.

    The returned function maps the value if it is Some.

  • none: (<T>() => Option<T>)

    Creates a new Option with the right-value None.

    const nothing = Option.none(); // None
    
      • <T>(): Option<T>
      • Type Parameters

        • T

        Returns Option<T>

        A new Option with the right-value None.

  • okOr: (<T, E>(error: E | Producer<E>) => Mapper<Option<T>, Result<T, E>>)

    Higher order function returning a function that performs the okOr method on the provided Option.

    The returned function maps the Option to a Result.

  • okOrError: (<T>(message: string | Producer<string>) => Mapper<Option<T>, Result<T, Error>>)

    Higher order function returning a function that performs the okOrError method on the provided Option.

    The returned function maps the Option to a Result, creating a new error from the message if the Option is None.

      • <T>(message): Mapper<Option<T>, Result<T, Error>>
      • Type Parameters

        • T

        Parameters

        • message: string | Producer<string>

          The message to create the error from if the Option is None.

        Returns Mapper<Option<T>, Result<T, Error>>

        A function that maps the Option to a Result.

  • transpose: (<T, E>(option: Option<Result<T, E>>) => Result<Option<T>, E>)

    Transposes an Option of a Result into a Result of an Option.

    const option = Option.some("123");                               // Some("123")
    const option_result = option.map(n => parseIntThrowingError(n)); // Some(Ok(123))
    const result_option = Option.transpose(option_result); // Ok(Some(123))

    // ...or when there's an error...

    const option = Option.some("a"); // Some("a")
    const option_result = option.map(n => parseIntThrowingError(n)); // Some(Err(ParseIntError))
    const result_option = Option.transpose(option_result); // Err(ParseIntError)
  • valueOr: (<T>(fallback: T | Producer<T>) => Mapper<Option<T>, T>)

    Higher order function returning a function that performs the valueOr method on the provided Option.

    The returned function returns the value if it is Some, otherwise returns a default value.

      • <T>(fallback): Mapper<Option<T>, T>
      • Type Parameters

        • T

        Parameters

        • fallback: T | Producer<T>

          The default value (as-is or produced from a callback) to return if the value is None.

        Returns Mapper<Option<T>, T>

        A function that returns the value if it is Some, otherwise the default value.