Improve indexBy type inference

This commit is contained in:
Charles-P. Clermont
2017-12-04 12:13:45 -05:00
parent 34f6a6d096
commit 06313d255b
2 changed files with 14 additions and 5 deletions

View File

@@ -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<T, U>(fn: (a: T) => string, list: T[]): U;
indexBy<T>(fn: (a: T) => string): <U>(list: T[]) => U;
indexBy<T>(fn: (a: T) => string, list: T[]): { [key: string]: T };
indexBy<T>(fn: (a: T) => string): (list: T[]) => { [key: string]: T };
/**
* Returns the position of the first occurrence of an item in an array

View File

@@ -678,10 +678,19 @@ interface Obj {
};
(() => {
const list = [{id: "xyz", title: "A"}, {id: "abc", title: "B"}];
const a1 = R.indexBy(R.prop<string>("id"), list);
const a2 = R.indexBy(R.prop<string>("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<string>("id"))(list);
const titlesIndexedByTitles: { [k: string]: string } = R.pipe(
R.map((x: Book) => x.title),
R.indexBy(x => x),
)(list);
});
() => {