type Identifiable = { id: string | number }; function indexById(items: readonly T[]): Map { return new Map(items.map(item => [item.id, item])); } const posts = indexById([ { id: 1, title: 'Hooks' }, { id: 2, title: 'Generics' }, ]); console.log(posts.get(2)?.title);