type InsertAtArgs = { index: number; element: T; }; export function insertAt(array: T[], args: InsertAtArgs) { const { index, element } = args; if (index >= array.length - 1) { const result = array.slice(); result.push(element); return result; } const result = array.slice(0, index); result.push(element); for (let i = 0; i < array.length - index; i++) { result.push(array[index + i]); } return result; }