Skip to content

Get-Or-Throw

Safe indexed access for TypeScript with noUncheckedIndexedAccess

What is Get-Or-Throw?

Get-Or-Throw provides a convenience function for safely accessing values in dynamic objects and arrays. It gets the value at a specified key or index, and throws an error if the resulting value is undefined.

This was created to make it easy to adhere to TypeScript's noUncheckedIndexedAccess setting, which is recommended for strict type checking.

ts
import { got } from "get-or-throw";

const arr = [1, 2, 3];
const arrValue = got(arr, 1); // 2 — type is `number`, not `number | undefined`

const obj = { a: 1, b: 2, c: 3 };
const objValue = got(obj, "b"); // 2

Instead of writing repetitive guard clauses or non-null assertions throughout your code, got() gives you a single, expressive call that either returns a defined value or throws a descriptive error.

Released under the MIT License.