From b17086bce7caf42cc125409566e61bfd522ed9e6 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Tue, 27 Jun 2017 16:21:57 -0700 Subject: [PATCH 1/2] Fix Ramda for 2.4: evolve and functor map 1. Evolve now uses mapped type to more accurately reflect the resulting type. 2. 2.4's contravariance checking of callbacks caught some incorrect code. I fixed one instance that was nearly-correct and deleted one instance that was non-sensical. It created an infinite functor by returning itself. --- types/ramda/index.d.ts | 13 +++++++------ types/ramda/ramda-tests.ts | 15 ++++----------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/types/ramda/index.d.ts b/types/ramda/index.d.ts index ba6908d57c..19f85adc74 100644 --- a/types/ramda/index.d.ts +++ b/types/ramda/index.d.ts @@ -55,15 +55,15 @@ declare namespace R { push(x: string): void; } - interface Nested { - [index: string]: Nested | ((value: any) => U); - } - interface Lens { (obj: T): U; set(str: string, obj: T): U; } + type Evolver = + | ((x: T) => T) + | { [K in keyof T]?: Evolver } + // @see https://gist.github.com/donnut/fd56232da58d25ceecf1, comment by @albrow interface CurriedTypeGuard2 { (t1: T1): (t2: T2) => t2 is R; @@ -567,8 +567,9 @@ declare namespace R { /** * Creates a new object by evolving a shallow copy of object, according to the transformation functions. */ - evolve(transformations: Nested, obj: V): Nested; - evolve(transformations: Nested): (obj: V) => Nested; + evolve(transformations: Evolver, obj: V): V; + evolve(transformations: Evolver): (obj: W) => W; + /* * A function that always returns false. Any passed in parameters are ignored. */ diff --git a/types/ramda/ramda-tests.ts b/types/ramda/ramda-tests.ts index 70298f53e6..18a5ed4e7d 100644 --- a/types/ramda/ramda-tests.ts +++ b/types/ramda/ramda-tests.ts @@ -744,13 +744,13 @@ interface Obj { R.map(double, [1, 2, 3]); // => [2, 4, 6] // functor - const stringFunctor = { - map: (fn: (c: number) => number) => { + const numberFunctor = { + map: (fn: (c: number) => U) => { let chars = "Ifmmp!Xpsme".split(""); - return chars.map((char) => String.fromCharCode(fn(char.charCodeAt(0)))).join("") as any; + return chars.map(char => fn(char.charCodeAt(0))); } }; - R.map((x: number) => x - 1, stringFunctor); // => "Hello World" + R.map((x: number) => x - 1, numberFunctor); // => "Hello World" }; () => { @@ -2199,10 +2199,3 @@ class Why { R.intersperse(0, [1, 2]); // => [1, 0, 2] R.intersperse(0, [1]); // => [1] }; - -{ - const functor = { - map: (fn: (x: string) => string) => functor - }; - R.map(x => x.trim(), functor); -} From 9558b1699a029abc25cc127c342c4ef6a7969927 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Tue, 27 Jun 2017 16:27:37 -0700 Subject: [PATCH 2/2] Fix lint --- types/ramda/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/ramda/index.d.ts b/types/ramda/index.d.ts index 19f85adc74..cb0241bea7 100644 --- a/types/ramda/index.d.ts +++ b/types/ramda/index.d.ts @@ -62,7 +62,7 @@ declare namespace R { type Evolver = | ((x: T) => T) - | { [K in keyof T]?: Evolver } + | { [K in keyof T]?: Evolver }; // @see https://gist.github.com/donnut/fd56232da58d25ceecf1, comment by @albrow interface CurriedTypeGuard2 {