Type-Safe Access
TypeScript assertions for type narrowing — the returned value is guaranteed to be defined.
Safe indexed access for TypeScript with noUncheckedIndexedAccess
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.
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"); // 2Instead 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.