typesafe-ts API Documentation - v1.2.0
    Preparing search index...

    Interface Optional<ValueType>

    The public interface of an Optional value. An Optional either contains a value of type ValueType or is empty.

    Optionals should be constructed using optional.some() or optional.none<ValueType>(). These functions return objects implementing the Optional interface.

    The value of an Optional can only be accessed after checking is_some().

    When working with Optional values, it is preferred to use methods such as map, and_then, and or_else for sequential operations, and only check is_some() at the end. These methods allow consumers to write type-safe code using functional patterns that are more expressive than handling empty states at every step.

    interface Optional<ValueType> {
        is_some(): this is OptionalWithValue<ValueType>;
        value_or(value_if_empty: ValueType): ValueType;
        map<NewValueType>(
            mapper_fn: (value: ValueType) => NewValueType,
        ): Optional<NewValueType>;
        and_then<NewValueType>(
            fn: (value: ValueType) => Optional<NewValueType>,
        ): Optional<NewValueType>;
        or_else(fn: () => Optional<ValueType>): Optional<ValueType>;
        "[iterator]"(): Generator<ValueType, void, unknown>;
        get "[toStringTag]"(): "Optional";
    }

    Type Parameters

    • ValueType

      The type of the value that the Optional may contain.

    Index

    Accessors

    Methods

    • Check if the Optional contains some value. This should be used in an if or switch to expose the value property.

      example:

      const maybeValue = optional.some("Hello");
      if (maybeValue.is_some()) {
      // maybeValue has been widened to OptionalWithValue<string> and has a `.value` property.
      console.log(maybeValue.value); // prints "Hello"
      } else {
      // Type error: Property 'Value' does not exist on type 'Optional<string>'.
      console.log(maybeValue.value);
      }

      @return true if the Optional contains a value and widens the type to

      Returns this is OptionalWithValue<ValueType>

    • Get the value of the Optional if contains some value. Otherwise, returns the provided default value.

      example:

      const maybeValue = optional.none<string>();
      console.log(maybeValue.value_or("Default")); // prints "Default"

      Parameters

      • value_if_empty: ValueType

        The value to return if the Optional is empty.

      Returns ValueType

      The value of the Optional if it contains a value, otherwise value_if_empty.

    • Transforms the value of the Optional using the provided function. If the Optional is empty, the mapping function will not be run.

      example:

      // with a value
      const maybe = optional.some("Hello").map((v) => v.toUpperCase());
      if (maybe.is_some()) {
      console.log(maybe.value); // prints "HELLO"
      }
      // without a value
      const empty = optional.none<string>().map((v) => v.toUpperCase());
      if (!empty.is_some()) {
      console.log("No value"); // prints "No value"
      }

      Type Parameters

      • NewValueType

      Parameters

      • mapper_fn: (value: ValueType) => NewValueType

        The function to transform the value, if present. Will not be called if the Optional is empty.

      Returns Optional<NewValueType>

      A new Optional containing the result of transforming the value or empty if this Optional is empty.

    • Chains another Optional operation if this optional contains some value. If this Optional is empty, the provided function will not be called.

      example:

      const maybe = optional.some("Hello").and_then((v) => optional.some(v.length));
      if (maybe.is_some()) {
      console.log(maybe.value); // prints 5
      }

      Type Parameters

      • NewValueType

      Parameters

      • fn: (value: ValueType) => Optional<NewValueType>

        A function that accepts an argument of the ValueType of this optional and that returns another Optional.

      Returns Optional<NewValueType>

      A new Optional containing the value from the provided function, or an empty Optional if this Optional is empty.

    • Provides a fallback Optional if this Optional is empty. If this Optional contains a value, it will return itself and the fallback function will not be called.

      example:

      const maybe = optional.none<string>().or_else(() => optional.some("Default"));
      if (maybe.is_some()) {
      console.log(maybe.value); // prints "Default"
      }

      Parameters

      • fn: () => Optional<ValueType>

        A function that returns an Optional to use as a fallback if this Optional is empty.

      Returns Optional<ValueType>

      The current Optional if it contains a value, otherwise the Optional returned by the provided function.

    • Returns a generator that yields the contained value if this Optional has a value. If this Optional is empty, the generator yields nothing (completes immediately). This allows for easy iteration over present values in for-of loops and other iterator contexts.

      Returns Generator<ValueType, void, unknown>

      A generator that yields the value if present, otherwise yields nothing

      const someValue = optional.some(42);
      for (const value of someValue) {
      console.log(value); // 42
      }

      const emptyValue = optional.none<number>();
      for (const value of emptyValue) {
      console.log("This won't execute");
      }

      // Useful for collecting present values from multiple optionals
      const optionals = [
      optional.some(1),
      optional.none<number>(),
      optional.some(3)
      ];

      const values = [];
      for (const opt of optionals) {
      for (const value of opt) {
      values.push(value);
      }
      }
      console.log(values); // [1, 3]