From 1550dfd1b8e38d9bf104b3fd16ea9bf98a2b358e Mon Sep 17 00:00:00 2001 From: liam-goodacre-hpe Date: Thu, 4 May 2017 01:18:14 +0100 Subject: [PATCH] Ramda - Fix typing of assoc, objOf, and transpose (#16091) * Fix typing of assoc, objOf, and transpose * Bump language version for Ramda --- types/ramda/index.d.ts | 15 ++++++++------- types/ramda/ramda-tests.ts | 26 +++++++++++++++----------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/types/ramda/index.d.ts b/types/ramda/index.d.ts index 7a4540e761..20ad9529a9 100644 --- a/types/ramda/index.d.ts +++ b/types/ramda/index.d.ts @@ -1,7 +1,8 @@ // Type definitions for ramda // Project: https://github.com/donnut/typescript-ramda -// Definitions by: Erwin Poeze , Matt DeKrey +// Definitions by: Erwin Poeze , Matt DeKrey , Liam Goodacre // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 declare var R: R.Static; @@ -214,9 +215,9 @@ declare namespace R { /** * Makes a shallow clone of an object, setting or overriding the specified property with the given value. */ - assoc(prop: string, val: T, obj: U): {prop: T} & U; - assoc(prop: string): (val: T, obj: U) => {prop: T} & U; - assoc(prop: string, val: T): (obj: U) => {prop: T} & U; + assoc(prop: K, val: T, obj: U): Record & U; + assoc(prop: K): (val: T, obj: U) => Record & U; + assoc(prop: K, val: T): (obj: U) => Record & U; /** @@ -1055,8 +1056,8 @@ declare namespace R { /** * Creates an object containing a single key:value pair. */ - objOf(key: string, value: T): {string: T}; - objOf(key: string): (value: T) => {string: T}; + objOf(key: K, value: T): Record; + objOf(key: K): (value: T) => Record; /** * Returns a singleton array containing the value provided. @@ -1584,7 +1585,7 @@ declare namespace R { /** * Transposes the rows and columns of a 2D list. When passed a list of n lists of length x, returns a list of x lists of length n. */ - transpose(list: any[][]): any[][]; + transpose(list: T[][]): T[][]; /** * Removes (strips) whitespace from both ends of the string. diff --git a/types/ramda/ramda-tests.ts b/types/ramda/ramda-tests.ts index a801a5696e..5c5948b047 100644 --- a/types/ramda/ramda-tests.ts +++ b/types/ramda/ramda-tests.ts @@ -965,9 +965,9 @@ type Pair = KeyValuePair } () => { - const a: any[][] = R.transpose([[1, 'a'], [2, 'b'], [3, 'c']]) //=> [[1, 2, 3], ['a', 'b', 'c']] - const b: any[][] = R.transpose([[1, 2, 3], ['a', 'b', 'c']]) //=> [[1, 'a'], [2, 'b'], [3, 'c']] - const c: any[][] = R.transpose([[10, 11], [20], [], [30, 31, 32]]) //=> [[10, 20, 30], [11, 31], [32]] + const a: (number | string)[][] = R.transpose([[1, 'a'], [2, 'b'], [3, 'c']]) //=> [[1, 2, 3], ['a', 'b', 'c']] + const b: (number | string)[][] = R.transpose([[1, 2, 3], ['a', 'b', 'c']]) //=> [[1, 'a'], [2, 'b'], [3, 'c']] + const c: number[][] = R.transpose([[10, 11], [20], [], [30, 31, 32]]) //=> [[10, 20, 30], [11, 31], [32]] } () => { @@ -1024,9 +1024,10 @@ type Pair = KeyValuePair * Object category */ () => { - const a = R.assoc('c', 3, {a: 1, b: 2}); //=> {a: 1, b: 2, c: 3} - const b = R.assoc('c')(3, {a: 1, b: 2}); //=> {a: 1, b: 2, c: 3} - const c = R.assoc('c', 3)({a: 1, b: 2}); //=> {a: 1, b: 2, c: 3} + type ABC = {a: number, b: number, c: number} + const a: ABC = R.assoc('c', 3, {a: 1, b: 2}); //=> {a: 1, b: 2, c: 3} + const b: ABC = R.assoc('c')(3, {a: 1, b: 2}); //=> {a: 1, b: 2, c: 3} + const c: ABC = R.assoc('c', 3)({a: 1, b: 2}); //=> {a: 1, b: 2, c: 3} } () => { @@ -1296,12 +1297,15 @@ class Rectangle { } () => { - var matchPhrases = R.compose( - R.objOf('must'), - R.map(R.objOf('match_phrase')) -) + const matchPhrases = (xs: string[]) => R.objOf('must', + R.map( + (x: string) => R.objOf('match_phrase', x), + xs + ) + ) -matchPhrases(['foo', 'bar', 'baz']); + const out: {must: Array<{match_phrase: string}>} = + matchPhrases(['foo', 'bar', 'baz']); } () => { R.omit(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, c: 3}