From 307c17d02732fd853c61f7e5fe3c43bf653cd567 Mon Sep 17 00:00:00 2001 From: AJ Richardson Date: Tue, 28 Nov 2017 21:47:23 -0500 Subject: [PATCH 1/3] lodash: add guards to allow certain functions to be used as iteratees. Also fixed _.pick overloads. --- types/lodash/index.d.ts | 138 +++++++++++++++++++++++++++++++++-- types/lodash/lodash-tests.ts | 51 +++++++++---- 2 files changed, 165 insertions(+), 24 deletions(-) diff --git a/types/lodash/index.d.ts b/types/lodash/index.d.ts index a0eaef941c..811d87c435 100644 --- a/types/lodash/index.d.ts +++ b/types/lodash/index.d.ts @@ -11667,14 +11667,30 @@ declare namespace _ { * @see _.random */ random( - min?: number, - floating?: boolean + max: number, + floating: boolean ): number; /** * @see _.random */ - random(floating?: boolean): number; + random(floating: boolean): number; + + /** + * Produces a random number between min and max (inclusive). If only one argument is provided a number between + * 0 and the given number is returned. If floating is true, or either min or max are floats, a floating-point + * number is returned instead of an integer. + * + * @param min The minimum possible value. + * @param index Not used in this overload. + * @param guard Enables use as an iteratee for methods like _.map. You should not pass this parameter directly in your code. + * @return Returns the random number. + */ + random( + min: number, + index: string | number, + guard: object + ): number; } interface LoDashImplicitWrapper { @@ -14394,7 +14410,7 @@ declare namespace _ { * // => { 'a': 1, 'c': 3 } */ pick( - object: T | null | undefined, + object: T, ...props: Array> ): Pick; @@ -14412,7 +14428,7 @@ declare namespace _ { * @see _.pick */ pick( - this: LoDashImplicitWrapper, + this: LoDashImplicitWrapper, ...props: Array> ): LoDashImplicitWrapper>; @@ -14430,7 +14446,7 @@ declare namespace _ { * @see _.pick */ pick( - this: LoDashExplicitWrapper, + this: LoDashExplicitWrapper, ...props: Array> ): LoDashExplicitWrapper>; @@ -15667,6 +15683,9 @@ declare namespace _ { * * Note: This method is based on String#split. * + * @param string The string to trim. + * @param separator The separator pattern to split by. + * @param limit The length to truncate results to. * @return Returns the new array of string segments. */ split( @@ -15674,6 +15693,22 @@ declare namespace _ { separator?: RegExp|string, limit?: number ): string[]; + + /** + * Splits string by separator. + * + * Note: This method is based on String#split. + * + * @param string The string to trim. + * @param index Not used in this overload. + * @param guard Enables use as an iteratee for methods like _.map. You should not pass this parameter directly in your code. + * @return Returns the new array of string segments. + */ + split( + string: string, + index: string | number, + guard: object + ): string[]; } interface LoDashImplicitWrapper { @@ -15881,6 +15916,20 @@ declare namespace _ { string?: string, chars?: string ): string; + + /** + * Removes leading and trailing whitespace or specified characters from string. + * + * @param string The string to trim. + * @param index Not used in this overload. + * @param guard Enables use as an iteratee for methods like _.map. You should not pass this parameter directly in your code. + * @return Returns the trimmed string. + */ + trim( + string: string, + index: string | number, + guard: object + ): string; } interface LoDashImplicitWrapper { @@ -15910,6 +15959,20 @@ declare namespace _ { string?: string, chars?: string ): string; + + /** + * Removes trailing whitespace or specified characters from string. + * + * @param string The string to trim. + * @param index Not used in this overload. + * @param guard Enables use as an iteratee for methods like _.map. You should not pass this parameter directly in your code. + * @return Returns the trimmed string. + */ + trimEnd( + string: string, + index: string | number, + guard: object + ): string; } interface LoDashImplicitWrapper { @@ -15939,6 +16002,20 @@ declare namespace _ { string?: string, chars?: string ): string; + + /** + * Removes leading whitespace or specified characters from string. + * + * @param string The string to trim. + * @param index Not used in this overload. + * @param guard Enables use as an iteratee for methods like _.map. You should not pass this parameter directly in your code. + * @return Returns the trimmed string. + */ + trimStart( + string: string, + index: string | number, + guard: object + ): string; } interface LoDashImplicitWrapper { @@ -16086,6 +16163,20 @@ declare namespace _ { string?: string, pattern?: string|RegExp ): string[]; + + /** + * Splits `string` into an array of its words. + * + * @param string The string to inspect. + * @param index Not used in this overload. + * @param guard Enables use as an iteratee for methods like _.map. You should not pass this parameter directly in your code. + * @return Returns the words of `string`. + */ + words( + string: string, + index: string | number, + guard: object + ): string[]; } interface LoDashImplicitWrapper { @@ -16746,6 +16837,22 @@ declare namespace _ { end?: number, step?: number ): number[]; + + /** + * Creates an array of numbers (positive and/or negative) progressing from start up to, but not including, end. + * If end is not specified it’s set to start with start then set to 0. If end is less than start a zero-length + * range is created unless a negative step is specified. + * + * @param start The start of the range. + * @param index Not used in this overload. + * @param guard Enables use as an iteratee for methods like _.map. You should not pass this parameter directly in your code. + * @return Returns a new range array. + */ + range( + end: number, + index: string | number, + guard: object + ): number[]; } interface LoDashImplicitWrapper { @@ -16775,9 +16882,9 @@ declare namespace _ { * descending order. * * @category Util - * @param [start=0] The start of the range. + * @param start The start of the range. * @param end The end of the range. - * @param [step=1] The value to increment or decrement by. + * @param step The value to increment or decrement by. * @returns Returns the new array of numbers. * @example * @@ -16807,6 +16914,21 @@ declare namespace _ { end?: number, step?: number ): number[]; + + /** + * This method is like _.range except that it populates values in + * descending order. + * + * @param start The start of the range. + * @param index Not used in this overload. + * @param guard Enables use as an iteratee for methods like _.map. You should not pass this parameter directly in your code. + * @return Returns a new range array. + */ + rangeRight( + end: number, + index: string | number, + guard: object + ): number[]; } interface LoDashImplicitWrapper { diff --git a/types/lodash/lodash-tests.ts b/types/lodash/lodash-tests.ts index dbefb1abaa..950f1eb112 100644 --- a/types/lodash/lodash-tests.ts +++ b/types/lodash/lodash-tests.ts @@ -9861,6 +9861,9 @@ namespace TestRandom { result = _(1).chain().random(true); result = _(true).chain().random(); } + + // $ExpectType number[] + _.map([5, 5], _.random); } /********** @@ -11808,50 +11811,51 @@ namespace TestOmitBy { // _.pick namespace TestPick { - let obj: TResult | null | undefined = any; + let obj1: TResult | null | undefined = any; + let obj2: TResult = any; { let result: Partial; - result = _.pick(obj, 'a'); - result = _.pick(obj, 0, 'a'); - result = _.pick(obj, ['b', 1], 0, 'a'); + result = _.pick(obj1, 'a'); + result = _.pick(obj1, 0, 'a'); + result = _.pick(obj1, ['b', 1], 0, 'a'); } { let result: Pick; - result = _.pick(obj, 'a', 'b'); - result = _.pick(obj, ['a' as 'a', 'b' as 'b']); + result = _.pick(obj2, 'a', 'b'); + result = _.pick(obj2, ['a' as 'a', 'b' as 'b']); } { let result: _.LoDashImplicitWrapper>; - result = _(obj).pick('a'); - result = _(obj).pick(0, 'a'); - result = _(obj).pick(['b', 1], 0, 'a'); + result = _(obj1).pick('a'); + result = _(obj1).pick(0, 'a'); + result = _(obj1).pick(['b', 1], 0, 'a'); } { let result: _.LoDashImplicitWrapper>; - result = _(obj).pick('a', 'b'); - result = _(obj).pick(['a' as 'a', 'b' as 'b']); + result = _(obj2).pick('a', 'b'); + result = _(obj2).pick(['a' as 'a', 'b' as 'b']); } { let result: _.LoDashExplicitWrapper>; - result = _(obj).chain().pick('a'); - result = _(obj).chain().pick(0, 'a'); - result = _(obj).chain().pick(['b', 1], 0, 'a'); + result = _(obj1).chain().pick('a'); + result = _(obj1).chain().pick(0, 'a'); + result = _(obj1).chain().pick(['b', 1], 0, 'a'); } { let result: _.LoDashExplicitWrapper>; - result = _(obj).chain().pick('a', 'b'); - result = _(obj).chain().pick(['a' as 'a', 'b' as 'b']); + result = _(obj2).chain().pick('a', 'b'); + result = _(obj2).chain().pick(['a' as 'a', 'b' as 'b']); } } @@ -12754,6 +12758,9 @@ namespace TestSplit { result = _('a-b-c').chain().split('-'); result = _('a-b-c').chain().split('-', 2); } + + // $ExpectType string[][] + _.map(['abc', 'def'], _.split); } // _.startCase @@ -12877,6 +12884,9 @@ namespace TestTrim { result = _('-_-abc-_-').chain().trim(); result = _('-_-abc-_-').chain().trim('_-'); } + + // $ExpectType string[] + _.map([' foo ', ' bar '], _.trim); } // _.trimEnd @@ -13013,6 +13023,9 @@ namespace TestWords { result = _('fred, barney, & pebbles').chain().words(); result = _('fred, barney, & pebbles').chain().words(/[^, ]+/g); } + + // $ExpectType string[][] + _.map(['fred, barney', 'pebbles'], _.words); } /*********** @@ -13831,6 +13844,9 @@ namespace TestRange { result = _(1).chain().range(11); result = _(0).chain().range(30, 5); } + + // $ExpectType number[][] + _.map([5, 5], _.range); } // _.rangeRight @@ -13858,6 +13874,9 @@ namespace TestRangeRight { result = _(1).chain().rangeRight(11); result = _(0).chain().rangeRight(30, 5); } + + // $ExpectType number[][] + _.map([5, 5], _.rangeRight); } // _.runInContext From 04ed785cc1097cf571b2a84fe5c0c7a7b9fa8ac1 Mon Sep 17 00:00:00 2001 From: AJ Richardson Date: Tue, 28 Nov 2017 21:58:55 -0500 Subject: [PATCH 2/3] lodash: _.partition should return a tuple instead of an array (#18350) --- types/lodash/index.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/types/lodash/index.d.ts b/types/lodash/index.d.ts index 811d87c435..544fe9c1ca 100644 --- a/types/lodash/index.d.ts +++ b/types/lodash/index.d.ts @@ -6263,7 +6263,7 @@ declare namespace _ { partition( collection: List | null | undefined, callback: ValueIteratee - ): T[][]; + ): [T[], T[]]; /** * @see _.partition @@ -6271,7 +6271,7 @@ declare namespace _ { partition( collection: T | null | undefined, callback: ValueIteratee - ): Array>; + ): [Array, Array]; } interface LoDashImplicitWrapper { @@ -6281,7 +6281,7 @@ declare namespace _ { partition( this: LoDashImplicitWrapper | null | undefined>, callback: ValueIteratee - ): LoDashImplicitWrapper; + ): LoDashImplicitWrapper<[T[], T[]]>; /** * @see _.partition @@ -6289,7 +6289,7 @@ declare namespace _ { partition( this: LoDashImplicitWrapper, callback: ValueIteratee - ): LoDashImplicitWrapper>>; + ): LoDashImplicitWrapper<[Array, Array]>; } interface LoDashExplicitWrapper { @@ -6299,7 +6299,7 @@ declare namespace _ { partition( this: LoDashExplicitWrapper | null | undefined>, callback: ValueIteratee - ): LoDashExplicitWrapper; + ): LoDashExplicitWrapper<[T[], T[]]>; /** * @see _.partition @@ -6307,7 +6307,7 @@ declare namespace _ { partition( this: LoDashExplicitWrapper, callback: ValueIteratee - ): LoDashExplicitWrapper>>; + ): LoDashExplicitWrapper<[Array, Array]>; } //_.reduce From 7021ff77ae54a830741fdc54783a729ab008d9a1 Mon Sep 17 00:00:00 2001 From: AJ Richardson Date: Thu, 14 Dec 2017 17:47:54 -0500 Subject: [PATCH 3/3] lodash: clean up random overloads --- types/lodash/index.d.ts | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/types/lodash/index.d.ts b/types/lodash/index.d.ts index 544fe9c1ca..2e34a259c4 100644 --- a/types/lodash/index.d.ts +++ b/types/lodash/index.d.ts @@ -11658,8 +11658,6 @@ declare namespace _ { * @return Returns the random number. */ random( - min?: number, - max?: number, floating?: boolean ): number; @@ -11668,13 +11666,17 @@ declare namespace _ { */ random( max: number, - floating: boolean + floating?: boolean ): number; /** * @see _.random */ - random(floating: boolean): number; + random( + min: number, + max: number, + floating?: boolean + ): number; /** * Produces a random number between min and max (inclusive). If only one argument is provided a number between @@ -11697,30 +11699,30 @@ declare namespace _ { /** * @see _.random */ - random( - max?: number, - floating?: boolean - ): number; + random(floating?: boolean): number; /** * @see _.random */ - random(floating?: boolean): number; + random( + max: number, + floating?: boolean + ): number; } interface LoDashExplicitWrapper { /** * @see _.random */ - random( - max?: number, - floating?: boolean - ): LoDashExplicitWrapper; + random(floating?: boolean): LoDashExplicitWrapper; /** * @see _.random */ - random(floating?: boolean): LoDashExplicitWrapper; + random( + max: number, + floating?: boolean + ): LoDashExplicitWrapper; } /**********