ts-fortress
    Preparing search index...

    Function fromOptional

    • Converts an Optional to a Result (the inverse of Result.toOptional, and the equivalent of Rust's Option::ok_or).

      If the Optional is Some, returns Ok with the contained value. If it is None, returns Err with the provided error value.

      Type Parameters

      • O extends UnknownOptional

        The input UnknownOptional type.

      • const E

        The type of the error value used for the None case.

      Parameters

      • optional: O

        The Optional to convert.

      • error: E

        The error value to use if the Optional is None.

      Returns Result<Unwrap<O>, E>

      Ok with the contained value, or Err with error.

      assert.deepStrictEqual(
      Result.fromOptional(Optional.some(42), 'missing'),
      Result.ok(42),
      );

      assert.deepStrictEqual(
      Result.fromOptional(Optional.none, 'missing'),
      Result.err('missing'),
      );

      const withMissingError = Result.fromOptional('missing');

      assert.deepStrictEqual(withMissingError(Optional.some(1)), Result.ok(1));

      assert.deepStrictEqual(
      withMissingError(Optional.none),
      Result.err('missing'),
      );
    • Converts an Optional to a Result (the inverse of Result.toOptional, and the equivalent of Rust's Option::ok_or).

      If the Optional is Some, returns Ok with the contained value. If it is None, returns Err with the provided error value.

      Type Parameters

      • const E

        The type of the error value used for the None case.

      Parameters

      • error: E

        The error value to use if the Optional is None.

      Returns <O extends UnknownOptional>(optional: O) => Result<Unwrap<O>, E>

      Ok with the contained value, or Err with error.

      assert.deepStrictEqual(
      Result.fromOptional(Optional.some(42), 'missing'),
      Result.ok(42),
      );

      assert.deepStrictEqual(
      Result.fromOptional(Optional.none, 'missing'),
      Result.err('missing'),
      );

      const withMissingError = Result.fromOptional('missing');

      assert.deepStrictEqual(withMissingError(Optional.some(1)), Result.ok(1));

      assert.deepStrictEqual(
      withMissingError(Optional.none),
      Result.err('missing'),
      );