Kit
Functions

unwrapOptionRecursively

Call Signature

unwrapOptionRecursively<T>(input): UnwrappedOption<T>

Recursively unwraps all nested Option types within a value.

This function traverses a given value and removes all instances of Option, replacing them with their contained values.

  • If an Option is encountered, its value is extracted.
  • If an array or object is encountered, its elements are traversed recursively.
  • If None is encountered, it is replaced with the fallback value (default: null).

Type Parameters

Type ParameterDescription
TThe type of the input value.

Parameters

ParameterTypeDescription
inputTThe value to unwrap.

Returns

UnwrappedOption<T>

The recursively unwrapped value.

Examples

Recursively unwrapping nested options.

unwrapOptionRecursively(some(some('Hello World'))); // "Hello World"
unwrapOptionRecursively(some(none<string>()));      // null

Recursively unwrapping options inside objects and arrays.

unwrapOptionRecursively({
  a: 'hello',
  b: none(),
  c: [{ c1: some(42) }, { c2: none() }],
});
// { a: "hello", b: null, c: [{ c1: 42 }, { c2: null }] }

Using a fallback value for None options.

unwrapOptionRecursively(
  {
    a: 'hello',
    b: none(),
    c: [{ c1: some(42) }, { c2: none() }],
  },
  () => 'Default',
);
// { a: "hello", b: "Default", c: [{ c1: 42 }, { c2: "Default" }] }

Remarks

This function does not mutate objects or arrays.

See

Call Signature

unwrapOptionRecursively<T, U>(input, fallback): UnwrappedOption<T, U>

Recursively unwraps all nested Option types within a value.

This function traverses a given value and removes all instances of Option, replacing them with their contained values.

  • If an Option is encountered, its value is extracted.
  • If an array or object is encountered, its elements are traversed recursively.
  • If None is encountered, it is replaced with the fallback value (default: null).

Type Parameters

Type ParameterDescription
TThe type of the input value.
UThe fallback type for None values (defaults to null).

Parameters

ParameterTypeDescription
inputTThe value to unwrap.
fallback() => UA function that provides a fallback value for None options.

Returns

UnwrappedOption<T, U>

The recursively unwrapped value.

Examples

Recursively unwrapping nested options.

unwrapOptionRecursively(some(some('Hello World'))); // "Hello World"
unwrapOptionRecursively(some(none<string>()));      // null

Recursively unwrapping options inside objects and arrays.

unwrapOptionRecursively({
  a: 'hello',
  b: none(),
  c: [{ c1: some(42) }, { c2: none() }],
});
// { a: "hello", b: null, c: [{ c1: 42 }, { c2: null }] }

Using a fallback value for None options.

unwrapOptionRecursively(
  {
    a: 'hello',
    b: none(),
    c: [{ c1: some(42) }, { c2: none() }],
  },
  () => 'Default',
);
// { a: "hello", b: "Default", c: [{ c1: 42 }, { c2: "Default" }] }

Remarks

This function does not mutate objects or arrays.

See

On this page