ts-data-forge
Version:
[](https://www.npmjs.com/package/ts-data-forge) [](https://www.npmjs.com/package/ts-data-forge) [ • 1.16 kB
JavaScript
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