Sobes.tech
Middle

Implementa la tipizzazione TypeScript per la funzione customMap, che accetta un array di qualsiasi tipo e una funzione di trasformazione che restituisce un altro tipo. La funzione di trasformazione deve accettare tre argomenti (come il callback in Array.map).

sobes.tech AI

Risposta dell'AI

typescript function customMap<T, U>( arr: T[], transformer: (value: T, index: number, array: T[]) => U ): U[] { const result: U[] = []; for (let i = 0; i < arr.length; i++) { result.push(transformer(arr[i], i, arr)); } return result; }


Здесь `T` — тип элементов исходного массива, `U` — тип элементов результата. Функция `transformer` принимает три аргумента: текущий элемент, индекс и исходный массив, и возвращает элемент типа `U`.