As per documentation, the function given to flatMap is only invoked if the status is Ok. Hence, if the Result was already in an error state BEFORE R.flatMap is invoked, the state doesn't change.
While this is true at runtime, this is not how the type specification is implemented.
For example:
import {R, pipe} from '@mobily/ts-belt';
class Error1 extends Error{}
class Error2 extends Error{}
const value = Math.random() < 0.5 ? null : 'test';
const test = pipe(
R.fromNullable(value, new Error1()),
R.flatMap((value) => {
return Math.random() < 0.5
? R.Ok(value)
: R.Error(new Error2())
}
)
)
console.log(test);
The type of test should be Result<string, Error1 | Error2>, but it is Result<string, Error2>.
As per documentation, the function given to flatMap is only invoked if the status is
Ok
. Hence, if theResult
was already in an error state BEFORER.flatMap
is invoked, the state doesn't change.While this is true at runtime, this is not how the type specification is implemented.
For example:
The type of
test
should beResult<string, Error1 | Error2>
, but it isResult<string, Error2>
.