Function: flatten()
flatten<
T
>(nested
):Maybe
<T
>
Given a nested Maybe
, remove one layer of nesting.
For example, given a Maybe<Maybe<string>>
, the resulting type after using this function will be Maybe<string>
.
Note
This function only works when the value wrapped in Maybe
is another Maybe
. If you have a Maybe<string>
or Maybe<number>
, this function won't work. If you have a Maybe<Maybe<string>>
, then you can call maybe.flatten(theMaybe)
to get back a Maybe<string>
.
Examples
ts
import * as maybe from 'true-myth/maybe';
const nested = maybe.just(maybe.just('hello'));
const flattened = maybe.flatten(nested); // Maybe<string>
console.log(flattened); // Just('hello')
const nestedNothing = maybe.just(maybe.nothing<string>());
const flattenedNothing = maybe.flatten(nestedNothing); // Maybe<string>
console.log(flattenedNothing); // Nothing
const nothingNested = maybe.nothing<Maybe<string>>();
const flattenedOuter = maybe.flatten(nothingNested); // Maybe<string>
console.log(flattenedOuter); // Nothing
Type Parameters
T
T
extends object
Parameters
nested
Returns
Maybe
<T
>