UNPKG

ts-data-forge

Version:

[![npm version](https://img.shields.io/npm/v/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![npm downloads](https://img.shields.io/npm/dm/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![License](https://img.shields.

36 lines 1.42 kB
import { type UnwrapErr, type UnwrapOk } from './types.mjs'; /** * Maps a `Result<S, E>` to `Result<S, E2>` by applying a function to the * error value. If the `Result` is `Result.Ok`, returns the original `Ok`. * * @example * * ```ts * const okValue = Result.ok(3) as Result<number, string>; * * const errValue = Result.err('missing'); * * const untouchedOk = Result.mapErr(okValue, (error) => error.toUpperCase()); * * const uppercasedErr = Result.mapErr(errValue, (error) => error.toUpperCase()); * * assert.deepStrictEqual(untouchedOk, Result.ok(3)); * * assert.deepStrictEqual(uppercasedErr, Result.err('MISSING')); * * const mapError = Result.mapErr((error: Readonly<Error>) => error.message); * * const wrapped = mapError(Result.err(new Error('boom'))); * * assert.deepStrictEqual(wrapped, Result.err('boom')); * ``` * * @template R The input `UnknownResult` type. * @template E2 The type of the error value returned by the mapping function. * @param result The `Result` to map. * @param mapFn The function to apply to the error value if present. * @returns A new `Result<UnwrapOk<R>, E2>`. */ export declare function mapErr<R extends UnknownResult, E2>(result: R, mapFn: (error: UnwrapErr<R>) => E2): Result<UnwrapOk<R>, E2>; export declare function mapErr<E, E2>(mapFn: (error: E) => E2): <S>(result: Result<S, E>) => Result<S, E2>; //# sourceMappingURL=result-map-err.d.mts.map