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.16 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'; /** * Returns the Ok value or throws if the result is Warn or Err. * * @example * * ```ts * const okValue = TernaryResult.ok('ready'); * * assert.strictEqual(TernaryResult.unwrapThrow(okValue), 'ready'); * * assert.throws( * () => TernaryResult.unwrapThrow(TernaryResult.warn('warn', 'warned')), * /Expected Ok/u, * ); * ``` */ const unwrapThrow = (result, toStr = unknownToString) => { if (isErr(result)) { throw new Error(`Expected Ok but got Err: ${toStr( // eslint-disable-next-line total-functions/no-unsafe-type-assertion result.value)}`); } if (isWarn(result)) { throw new Error(`Expected Ok but got Warn: ${toStr( // eslint-disable-next-line total-functions/no-unsafe-type-assertion result.warning)}`); } // eslint-disable-next-line total-functions/no-unsafe-type-assertion return result.value; }; export { unwrapThrow }; //# sourceMappingURL=ternary-result-unwrap-throw.mjs.map