get-or-throw
Version:
A convenience function for safely getting values from dynamic objects and arrays
16 lines (14 loc) • 787 B
text/typescript
/**
* Get a value from an object or array, and throw an error if the key or index
* does not exist or if the resulting value is undefined.
*
* @param objOrArr The object or array to get the value from.
* @param keyOrIndex The key or index to get the value from.
* @param errorMessage Optional error message to include in the error thrown.
* @returns The value at the given key or index, guaranteed to be defined.
* @throws An error if the key or index does not exist, or if the value is
* undefined.
*/
declare function getOrThrow<T extends object, K extends keyof T>(objOrArr: T, keyOrIndex: K, errorMessage?: string): NonNullable<T[K]>;
declare function getOrThrow<T>(objOrArr: T[], keyOrIndex: number, errorMessage?: string): T;
export { getOrThrow, getOrThrow as got };