Function: flatten()
flatten<
T
,E1
,E2
>(nested
):Result
<T
,E1
|E2
>
Given a nested Result
, remove one layer of nesting.
For example, given a Result<Result<string, E2>, E1>
, the resulting type after using this function will be Result<string, E1 | E2>
.
Note
This function only works when the value wrapped in Result
is another Result
. If you have a Result<string, E>
or Result<number, E>
, this function won't work. If you have a Result<Result<string, E2>, E1>
, then you can call result.flatten(theResult)
to get back a Result<string, E1 | E2>
.
Examples
ts
import * as result from 'true-myth/result';
const nested = result.ok(result.ok('hello'));
const flattened = result.flatten(nested); // Result<string, never>
console.log(flattened); // Ok('hello')
const nestedError = result.ok(result.err('inner error'));
const flattenedError = result.flatten(nestedError); // Result<never, string>
console.log(flattenedError); // Err('inner error')
const errorNested = result.err<Result<string, string>, string>('outer error');
const flattenedOuter = result.flatten(errorNested); // Result<string, string>
console.log(flattenedOuter); // Err('outer error')
Type Parameters
T
T
E1
E1
E2
E2
Parameters
nested
Returns
Result
<T
, E1
| E2
>