mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-06-28 14:20:12 +00:00
Improve indexBy type inference
This commit is contained in:
4
types/ramda/index.d.ts
vendored
4
types/ramda/index.d.ts
vendored
@@ -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
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
() => {
|
||||
|
||||
Reference in New Issue
Block a user