Skip to content

Function: mapOk()

Call Signature

mapOk<R, MapperReturn>(fn): (result) => Result<InferValueAsOk<MapperReturn>, UnwrapErr<R | MapperReturn>>

Defined in: fp.ts:60

Takes a mapper function and returns a function that accepts a Result. The returned function transforms the data for Ok results using the mapper, while passing Err results unchanged.

Type Parameters

R

R extends Result

The input Result type

MapperReturn

MapperReturn

The type returned by the mapper function

Parameters

fn

(data) => NonPromise<MapperReturn>

Function to transform the Ok data

Returns

A function that takes a Result and returns a new Result with transformed data

(result): Result<InferValueAsOk<MapperReturn>, UnwrapErr<R | MapperReturn>>

Parameters

result

NonPromise<R>

Returns

Result<InferValueAsOk<MapperReturn>, UnwrapErr<R | MapperReturn>>

Example

typescript
const result = pipe(
  ok(42),
  mapOk((x) => x * 2),         // Ok<84>
  mapOk((x) => x.toString()),  // Ok<"84">
);

const errorResult = pipe(
  err("network error"),
  mapOk((x) => x * 2),         // Err<"network error"> (unchanged)
  mapOk((x) => x.toString())   // Err<"network error"> (unchanged)
);

Call Signature

mapOk<R, MapperReturn>(fn): (result) => Promise<Result<InferValueAsOk<MapperReturn>, UnwrapErr<R | MapperReturn>>>

Defined in: fp.ts:63

Takes a mapper function and returns a function that accepts a Result. The returned function transforms the data for Ok results using the mapper, while passing Err results unchanged.

Type Parameters

R

R extends Result

The input Result type

MapperReturn

MapperReturn

The type returned by the mapper function

Parameters

fn

(data) => OrPromise<MapperReturn>

Function to transform the Ok data

Returns

A function that takes a Result and returns a new Result with transformed data

(result): Promise<Result<InferValueAsOk<MapperReturn>, UnwrapErr<R | MapperReturn>>>

Parameters

result

OrPromise<R>

Returns

Promise<Result<InferValueAsOk<MapperReturn>, UnwrapErr<R | MapperReturn>>>

Example

typescript
const result = pipe(
  ok(42),
  mapOk((x) => x * 2),         // Ok<84>
  mapOk((x) => x.toString()),  // Ok<"84">
);

const errorResult = pipe(
  err("network error"),
  mapOk((x) => x * 2),         // Err<"network error"> (unchanged)
  mapOk((x) => x.toString())   // Err<"network error"> (unchanged)
);