Exploring Option Getters in Effect-TS
Tags: typescript, javascript, functional, effect
Published: 2024-07-16 17:55:06

Effect-TS provides a robust set of tools for handling Option types, which represent values that may or may not exist. In this article, we'll explore various ways to get the value inside an Option using different getters provided by the library.

## Example 1: Using `O.getOrElse`

The `O.getOrElse` function allows you to provide a default value if the Option is None. This is useful when you want to ensure a fallback value is always available.

```typescript
import { Option as O, pipe } from 'effect';

function getters_ex01() {
  const some = O.some(1); // Create an Option containing the value 1
  const none = O.none(); // Create an Option representing no value

  console.log(pipe(some, O.getOrElse(() => 'none'))); // Output: 1 (since some contains 1)
  console.log(pipe(none, O.getOrElse(() => 'none'))); // Output: 'none' (since none is None)
}
```

## Example 2: Using `O.getOrThrow`

The O.getOrThrow function returns the value inside the Option if it is Some, otherwise it throws a default error.

```typescript
import { Option as O, pipe } from 'effect';

function getters_ex02() {
  const some = O.some(1); // Create an Option containing the value 1
  const none = O.none(); // Create an Option representing no value

  console.log(pipe(some, O.getOrThrow)); // Output: 1 (since some contains 1)
  
  try {
    console.log(pipe(none, O.getOrThrow)); // This will throw an error
  } catch (e) {
    console.log(e.message); // Output: getOrThrow called on a None
  }
}
```

## Example 3: Using `O.getOrNull`

The `O.getOrNull` function returns the value inside the Option if it is Some, otherwise it returns null.


```typescript
import { Option as O, pipe } from 'effect';

function getters_ex03() {
  const some = O.some(1); // Create an Option containing the value 1
  const none = O.none(); // Create an Option representing no value

  console.log(pipe(some, O.getOrNull)); // Output: 1 (since some contains 1)
  console.log(pipe(none, O.getOrNull)); // Output: null (since none is None)
}
```

## Example 4: Using `O.getOrUndefined`

The `O.getOrUndefined` function returns the value inside the Option if it is Some, otherwise it returns undefined.

```typescript
import { Option as O, pipe } from 'effect';

function getters_ex04() {
  const some = O.some(1); // Create an Option containing the value 1
  const none = O.none(); // Create an Option representing no value

  console.log(pipe(some, O.getOrUndefined)); // Output: 1 (since some contains 1)
  console.log(pipe(none, O.getOrUndefined)); // Output: undefined (since none is None)
}
```

## Example 5: Using `O.getOrThrowWith`

```typescript
import { Option as O, pipe } from 'effect';

function getters_ex05() {
  const some = O.some(1); // Create an Option containing the value 1
  const none = O.none(); // Create an Option representing no value

  console.log(pipe(some, O.getOrThrowWith(() => new Error('Custom Error')))); // Output: 1 (since some contains 1)
  
  try {
    console.log(pipe(none, O.getOrThrowWith(() => new Error('Custom Error')))); // This will throw a custom error
  } catch (e) {
    console.log(e.message); // Output: Custom Error
  }
}
```

## Conclusion

By using these different getters, you can effectively handle `Option` types in various scenarios, ensuring your code behaves correctly whether an `Option` is `Some` or `None`. These utilities provide a clear, type-safe way to work with optional values, avoiding common pitfalls associated with null checks and enhancing the readability and maintainability of your code. Adopting these patterns can lead to cleaner, more robust codebases where