From 06313d255bb108839fe9691a2ebe7f8043bb303d Mon Sep 17 00:00:00 2001 From: "Charles-P. Clermont" Date: Mon, 4 Dec 2017 12:13:45 -0500 Subject: [PATCH] Improve indexBy type inference --- types/ramda/index.d.ts | 4 ++-- types/ramda/ramda-tests.ts | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/types/ramda/index.d.ts b/types/ramda/index.d.ts index 9d1683fbbc..de4a05b28f 100644 --- a/types/ramda/index.d.ts +++ b/types/ramda/index.d.ts @@ -740,8 +740,8 @@ declare namespace R { * Given a function that generates a key, turns a list of objects into an object indexing the objects * by the given key. */ - indexBy(fn: (a: T) => string, list: T[]): U; - indexBy(fn: (a: T) => string): (list: T[]) => U; + indexBy(fn: (a: T) => string, list: T[]): { [key: string]: T }; + indexBy(fn: (a: T) => string): (list: T[]) => { [key: string]: T }; /** * Returns the position of the first occurrence of an item in an array diff --git a/types/ramda/ramda-tests.ts b/types/ramda/ramda-tests.ts index 426bb63471..8e91b12ef2 100644 --- a/types/ramda/ramda-tests.ts +++ b/types/ramda/ramda-tests.ts @@ -678,10 +678,19 @@ interface Obj { }; (() => { - const list = [{id: "xyz", title: "A"}, {id: "abc", title: "B"}]; - const a1 = R.indexBy(R.prop("id"), list); - const a2 = R.indexBy(R.prop("id"))(list); + interface Book { + id: string; + title: string; + } + const list: Book[] = [{id: "xyz", title: "A"}, {id: "abc", title: "B"}]; + const a1 = R.indexBy(R.prop("id"), list); + const a2 = R.indexBy(R.prop("id"))(list); const a3 = R.indexBy<{ id: string }>(R.prop("id"))(list); + + const titlesIndexedByTitles: { [k: string]: string } = R.pipe( + R.map((x: Book) => x.title), + R.indexBy(x => x), + )(list); }); () => {