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.

44 lines (41 loc) 1.49 kB
import { unknownToString } from '../../../others/unknown-to-string.mjs'; import { isErr } from './result-is-err.mjs'; /** * Unwraps a `Result`, returning the success value. Throws an error if the * `Result` is `Result.Err`. * * This is useful when you're confident that a Result should contain a success * value and want to treat errors as exceptional conditions. The error message * will be constructed from the error value using the provided string * conversion function. * * @example * * ```ts * const okResult = Result.ok('data'); * * const errResult = Result.err(new Error('fail')); * * assert.isTrue(Result.unwrapThrow(okResult) === 'data'); * * assert.throws(() => Result.unwrapThrow(errResult), /fail/u); * ``` * * @template R The `UnknownResult` type to unwrap. * @param result The `Result` to unwrap. * @param toStr An optional function to convert the error value to a string * for the error message. Defaults to `String`. * @returns The success value if `Result.Ok`. * @throws {Error} Error with the stringified error value if the `Result` is * `Result.Err`. */ const unwrapThrow = (result, toStr = unknownToString) => { if (isErr(result)) { // eslint-disable-next-line total-functions/no-unsafe-type-assertion throw new Error(toStr(result.value)); } // eslint-disable-next-line total-functions/no-unsafe-type-assertion return result.value; }; export { unwrapThrow }; //# sourceMappingURL=result-unwrap-throw.mjs.map