export class SafeArray extends Array { public static override from(array: ArrayLike): SafeArray { return new SafeArray(array); } constructor(array: ArrayLike) { super(array.length); for (const index in array) { const value = array[index] if (value === undefined) { throw new Error('Array passed to constructor has undefined values') } this[index] = value; } } public toArray(): T[] { return [...this]; } public override map(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: unknown): SafeArray { return new SafeArray(super.map(callbackfn, thisArg)); } public override filter(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: unknown): SafeArray { return new SafeArray(super.filter(callbackfn, thisArg)); } public get(this: SafeArray, index: number): T { const value = this.at(index); if (value === undefined) { throw new Error(`Element at index ${index} is undefined.`) } return value } public last(this: SafeArray): T { return this.get(this.length - 1) } public first(this: SafeArray): T { return this.get(0) } } export function safeGet(obj: Partial>, key: string, errorMessage?: string) { const value = obj[key]; if (value === undefined) throw new Error(`Key '${key.toString()}' not found in object. Available keys: ${Object.keys(obj).join(', ')}.${errorMessage ? ` ${errorMessage}` : ''}`); return value; } const prefix = 'Requirement not met'; export function must(condition: unknown, message?: string | (() => string)): asserts condition { if (condition) return; const provided = typeof message === 'function' ? message() : message; const value = provided ? `${prefix}: ${provided}` : prefix; throw new Error(value); }