ts-data-forge
Version:
[](https://www.npmjs.com/package/ts-data-forge) [](https://www.npmjs.com/package/ts-data-forge) [ • 1.23 kB
JavaScript
import { isSome } from './optional-is-some.mjs';
/**
* Unwraps an `Optional`, returning the contained value. Throws an error if
* the `Optional` is `None`.
*
* This is a safer alternative to direct value access when you know the
* Optional should contain a value. Use this method when an empty Optional
* represents a programming error or unexpected condition.
*
* @example
*
* ```ts
* const present = Optional.some('available');
*
* assert.isTrue(Optional.unwrapThrow(present) === 'available');
*
* assert.throws(
* () => Optional.unwrapThrow(Optional.none),
* /has failed because it is `None`/u,
* );
* ```
*
* @template O The `UnknownOptional` type to unwrap.
* @param optional The `Optional` to unwrap.
* @returns The contained value if `Some`.
* @throws {Error} Error with message "`unwrapThrow()` has failed because it
* is `None`" if the `Optional` is `None`.
*/
const unwrapThrow = (optional) => {
if (isSome(optional)) {
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
return optional.value;
}
throw new Error('`unwrapThrow()` has failed because it is `None`');
};
export { unwrapThrow };
//# sourceMappingURL=optional-unwrap-throw.mjs.map