ts-data-forge
    Preparing search index...
    • Unwraps an Optional, returning the contained value or a default value if it's None.

      Supports both direct usage and curried form for functional composition. This is often preferred over unwrap() when you have a sensible fallback value.

      Type Parameters

      • O extends UnknownOptional

        The UnknownOptional type to unwrap.

      • D

        The type of the default value.

      Parameters

      • optional: O

        The Optional to unwrap.

      • defaultValue: D

        The value to return if optional is None.

      Returns D | Unwrap<O>

      The contained value if Some, otherwise defaultValue.

      const withValue = Optional.some(5);

      const withoutValue = Optional.none as Optional<number>;

      assert.isTrue(Optional.unwrapOr(withValue, 0) === 5);

      assert.isTrue(Optional.unwrapOr(withoutValue, 0) === 0);

      const unwrapWithDefault = Optional.unwrapOr(10);

      assert.isTrue(unwrapWithDefault(Optional.some(3)) === 3);

      assert.isTrue(unwrapWithDefault(Optional.none) === 10);
    • Unwraps an Optional, returning the contained value or a default value if it's None.

      Supports both direct usage and curried form for functional composition. This is often preferred over unwrap() when you have a sensible fallback value.

      Type Parameters

      • S
      • D

        The type of the default value.

      Parameters

      • defaultValue: D

        The value to return if optional is None.

      Returns (optional: Optional<S>) => S | D

      The contained value if Some, otherwise defaultValue.

      const withValue = Optional.some(5);

      const withoutValue = Optional.none as Optional<number>;

      assert.isTrue(Optional.unwrapOr(withValue, 0) === 5);

      assert.isTrue(Optional.unwrapOr(withoutValue, 0) === 0);

      const unwrapWithDefault = Optional.unwrapOr(10);

      assert.isTrue(unwrapWithDefault(Optional.some(3)) === 3);

      assert.isTrue(unwrapWithDefault(Optional.none) === 10);