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.

38 lines (35 loc) 1.23 kB
import { unknownToString } from '../../../others/unknown-to-string.mjs'; import { isErr } from './ternary-result-is-err.mjs'; import { isWarn } from './ternary-result-is-warn.mjs'; import { variantName } from './variant-name.mjs'; /** * Returns the Err value or throws if the result is not Err. * * @example * * ```ts * const errValue = TernaryResult.err('boom'); * * assert.strictEqual(TernaryResult.unwrapErrThrow(errValue), 'boom'); * * assert.throws( * () => TernaryResult.unwrapErrThrow(TernaryResult.ok('value')), * /Expected Err/u, * ); * ``` */ const unwrapErrThrow = (result, toStr = unknownToString) => { if (isErr(result)) { // eslint-disable-next-line total-functions/no-unsafe-type-assertion return result.value; } const variant = variantName(result.$$tag); const payload = isWarn(result) ? // eslint-disable-next-line total-functions/no-unsafe-type-assertion result.warning : // eslint-disable-next-line total-functions/no-unsafe-type-assertion result.value; throw new Error(`Expected Err but got ${variant}: ${toStr(payload)}`); }; export { unwrapErrThrow }; //# sourceMappingURL=ternary-result-unwrap-err-throw.mjs.map