type DictionaryKey = string | number; type GetKey = (item: T) => unknown; type Key, R = ReturnType> = R extends DictionaryKey ? R : never; type ArrayToDictionary = { >(array: T[], getKey: Fn, multiple?: false): Record, T>; >(array: T[], getKey: Fn, multiple: true): Record, T[]>; }; /** * @param array Array to be converted to dictionary * @param getKey Function extracting string from an array item T, which will become its * key in the dictionary (if not unique, latter item could replace the former one) * Item will not be added to dictionary if key is not defined * @param multiple If true, dictionary values are arrays of all items with the given key * @returns Dictionary object with array items as values */ const validateKey = (key: unknown): key is DictionaryKey => { if (['string', 'number'].includes(typeof key)) { return true; } return false; }; export const arrayToDictionary: ArrayToDictionary = >( array: T[], getKey: Fn, multiple?: boolean, ) => multiple ? array.reduce>((prev, cur) => { const key = getKey(cur); if (validateKey(key)) { const group = prev[key]; if (group) { group.push(cur); } else { prev[key] = [cur]; } } return prev; }, {}) : array.reduce>((prev, cur) => { const key = getKey(cur); if (validateKey(key)) { prev[key] = cur; } return prev; }, {});