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.

40 lines (37 loc) 1.23 kB
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