API reference › @evolu/common › Result › flatMapResult
function flatMapResult<T, E, U, F>(
result: Result<T, E>,
fn: (value: T) => Result<U, F>,
): Result<U, E | F>;
Defined in: packages/common/src/Result.ts:947
Composes a successful Result with another Result-returning operation.
Returns the existing error without calling the operation when the Result has failed.
Do not nest flatMapResult. For longer workflows, use explicit checks, which
keep names, intermediate values, and control flow flat and easy to read.
Example
import {
assertOk,
assertType,
flatMapResult,
ok,
type Result,
type Typed,
} from "@evolu/common";
interface User {
readonly id: string;
}
interface UserNotFoundError extends Typed<"UserNotFound"> {}
interface Profile {
readonly userId: string;
}
const getProfile = (userId: string): Result<Profile, ProfileNotFoundError> =>
ok({ userId });
interface ProfileNotFoundError extends Typed<"ProfileNotFound"> {}
const user: Result<User, UserNotFoundError> = ok({ id: "user-1" });
const profile = flatMapResult(user, ({ id }) => getProfile(id));
assertType<
Result<Profile, UserNotFoundError | ProfileNotFoundError>,
typeof profile
>();
assertOk(profile, { userId: "user-1" });