Hone logo
Hone
Problems

Implementing an Option/Maybe Type in TypeScript

Functional programming often deals with situations where a value might be present or absent. Instead of using null or undefined, which can lead to runtime errors, the Option (or Maybe) type provides a safer and more explicit way to represent the potential absence of a value. This challenge asks you to implement a basic Option type in TypeScript to handle such scenarios gracefully.

Problem Description

You are tasked with creating a TypeScript Option type that encapsulates a value which may or may not be present. The Option type should provide methods to safely access the contained value and handle the case where the value is absent. The core idea is to avoid null or undefined checks and instead use the Option type to explicitly represent the possibility of a missing value.

Key Requirements:

  • Constructor: An Option constructor that accepts a value (or nothing, representing absence).
  • isSome() method: A method that returns true if the Option contains a value, and false otherwise.
  • isNone() method: A method that returns true if the Option does not contain a value, and false otherwise.
  • get() method: A method that returns the contained value if it exists. If the Option is empty (doesn't contain a value), it should throw an error (e.g., Error("Option is None")).
  • map() method: A method that takes a function as an argument. If the Option contains a value, it applies the function to the value and returns a new Option containing the result. If the Option is empty, it returns a new empty Option.
  • flatMap() method: A method that takes a function as an argument. This function should return another Option. If the current Option contains a value, the function is applied to it, and the resulting Option is returned. If the current Option is empty, an empty Option is returned. This is useful for chaining operations that might themselves return Option values.

Expected Behavior:

The Option type should behave as a container for a value that might be present or absent. Methods should provide safe ways to interact with the contained value, avoiding direct null or undefined checks. The map and flatMap methods should allow for chaining operations on potentially absent values.

Edge Cases to Consider:

  • Creating an Option with no initial value (representing absence).
  • Calling get() on an empty Option.
  • Chaining map or flatMap operations.
  • Handling functions passed to map and flatMap that might themselves return null or undefined.

Examples

Example 1:

Input: new Option(5)
Output: Option { value: 5 }
Explanation: Creates an Option containing the value 5.

Example 2:

Input: new Option<string>(null)
Output: Option { value: null }
Explanation: Creates an Option containing the value null.  (Important to handle null correctly)

Example 3:

const option1 = new Option(10);
const option2 = option1.map(x => x * 2);
console.log(option2.get()); // Output: 20

const option3 = new Option<number>(null);
const option4 = option3.map(x => x * 2);
console.log(option4.isNone()); // Output: true

Explanation: Demonstrates map operation. If the initial option has a value, the mapping function is applied. If the initial option is empty, the resulting option is also empty.

Example 4:

const option1 = new Option(5);
const option2 = option1.flatMap(x => new Option(x + 5));
console.log(option2.get()); // Output: 10

const option3 = new Option<number>(null);
const option4 = option3.flatMap(x => new Option(x + 5));
console.log(option4.isNone()); // Output: true

Explanation: Demonstrates flatMap operation. If the initial option has a value, the mapping function (which returns an Option) is applied. If the initial option is empty, the resulting option is also empty.

Constraints

  • The Option type should be generic, allowing it to hold values of any type.
  • The get() method must throw an error if the Option is empty. The error message should be descriptive (e.g., "Option is None").
  • The map and flatMap methods should return a new Option instance, rather than modifying the existing one.
  • The code should be well-documented and easy to understand.

Notes

  • Consider using TypeScript's generics to make the Option type type-safe.
  • Think about how to handle the case where the function passed to map or flatMap returns null or undefined. Should these be treated as "None" values? (For this challenge, treat them as "None" values).
  • This is a simplified implementation of the Option type. Real-world implementations might include additional methods for error handling and more complex scenarios.
  • Focus on clarity and correctness over extreme performance optimization. The goal is to demonstrate understanding of the concept.
Loading editor...
typescript