15 lines
530 B
TypeScript
15 lines
530 B
TypeScript
/** Assert required input without turning missing data into NaN or a later TypeError. */
|
|
export function defined<T>(value: T | null | undefined, message = 'Required value is missing'): T {
|
|
if (value === undefined || value === null) {
|
|
throw new Error(message);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
export function at<T>(values: ArrayLike<T>, index: number): T {
|
|
const value = values[index];
|
|
if (value === undefined) {
|
|
throw new RangeError(`Missing array element at index ${index} (length ${values.length})`);
|
|
}
|
|
return value;
|
|
}
|