Skip to main content
The Deno 2 Release Candidate is here
Learn more
Module

x/yano3nora_tsutils/main.ts>recordBy

TypeScript Utilities as a npm dual package by Deno.
Latest
variable recordBy
import { recordBy } from "https://deno.land/x/yano3nora_tsutils@v0.29.0/main.ts";

Examples

type Keys = 1 | 2 const obj = recordBy<Keys, { id: Keys, name: string }>( item => item.id, [ { id: 1, name: 'john' }, { id: 2, name: 'jane' }, ], ) // => { 1: { id: 1, name: 'john' }, 2: { id: 2, name: 'jane' } }

obj[1].name // john obj[3].name // type error

type

<K extends PropertyKey, T extends Record<PropertyKey, unknown>>(getKey: (item: T) => K, objects: T[]) => Record<K, T>