@expo/nullthrows
Version:
An idiomatic way to enforce values not to be null nor undefined, with first-class support for TypeScript and ES modules
16 lines • 697 B
JavaScript
/**
* Enforces the given value to be neither `null` nor `undefined` and throws a {@link TypeError}
* otherwise.
* @param value the value to enforce not to be `null` or `undefined`
* @param message an optional error message to use in the thrown `TypeError` if the given value is
* `null` or `undefined`
* @returns the given value if it is neither `null` nor `undefined`
* @throws {@link TypeError} if the given value is `null` or `undefined`
*/
export default function nullthrows(value, message) {
if (value != null) {
return value;
}
throw new TypeError(message ?? `Expected value not to be null or undefined but got ${value}`);
}
//# sourceMappingURL=nullthrows.js.map