Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
Source Link
Baptiste Arnaud

Here is the modern (Typescript functional) way:

export const insertItemInList = <T>(
  arr: T[],
  index: number,
  newItem: T
): T[] => [...arr.slice(0, index), newItem, ...arr.slice(index)]
lang-js