Skip to content

returnOrThrowError

Return a value or throw an error if null or undefined.

Available since v1.0.0

import { returnOrThrowError } from '@helpers4/function';
returnOrThrowError<T>(value: T | null | undefined, error: string): T
ParameterTypeDescription
valueT | null | undefinedA possible non-defined value.
errorstringThe error message to throw.

T — A defined value or an error.

Returns the value when it is defined and not null.

returnOrThrowError('hello', 'Value is missing')
// => 'hello'

Throws an error when the value is null or undefined.

returnOrThrowError(null, 'Value is missing')
// throws Error('Value is missing')

View source on GitHub