From be8fece482fe3204544e690b6462beeb51b23ae4 Mon Sep 17 00:00:00 2001 From: AJ Richardson Date: Fri, 18 Aug 2017 09:55:13 -0400 Subject: [PATCH] [lodash] improved iteratee parameters, simplified forEach and flatMap overloads (#18663) * [lodash] improved iterateee parameters, simplified forEach overloads * Changed ListIterator to StringIterator, cleaned up more issues with flatMap * Even more flatMap cleanup * Fixed type inference for forEach callback arguments --- types/lodash/index.d.ts | 849 ++++++++--------------------------- types/lodash/lodash-tests.ts | 452 ++++++++++++++----- 2 files changed, 512 insertions(+), 789 deletions(-) diff --git a/types/lodash/index.d.ts b/types/lodash/index.d.ts index 281c048fb6..301f4d8b66 100644 --- a/types/lodash/index.d.ts +++ b/types/lodash/index.d.ts @@ -6368,101 +6368,7 @@ declare namespace _ { //_.each interface LoDashStatic { - /** - * @see _.forEach - */ - each( - collection: T[], - iteratee?: ListIterator - ): T[]; - - /** - * @see _.forEach - */ - each( - collection: string, - iteratee?: ListIterator - ): string; - - /** - * @see _.forEach - */ - each( - collection: List, - iteratee?: ListIterator - ): List; - - /** - * @see _.forEach - */ - each( - collection: Dictionary, - iteratee?: DictionaryIterator - ): Dictionary; - - /** - * @see _.forEach - */ - each( - collection: T, - iteratee?: ObjectIterator - ): T; - - /** - * @see _.forEach - */ - each( - collection: T, - iteratee?: ObjectIterator - ): T; - - /** - * @see _.forEach - */ - each( - collection: T[] | null | undefined, - iteratee?: ListIterator - ): T[] | null | undefined; - - /** - * @see _.forEach - */ - each( - collection: string | null | undefined, - iteratee?: ListIterator - ): string | null | undefined; - - /** - * @see _.forEach - */ - each( - collection: List | null | undefined, - iteratee?: ListIterator - ): List | null | undefined; - - /** - * @see _.forEach - */ - each( - collection: Dictionary | null | undefined, - iteratee?: DictionaryIterator - ): Dictionary | null | undefined; - - /** - * @see _.forEach - */ - each( - collection: T | null | undefined, - iteratee?: ObjectIterator - ): T | null | undefined; - - /** - * @see _.forEach - */ - each( - collection: T | null | undefined, - iteratee?: ObjectIterator - ): T | null | undefined; + each: typeof _.forEach; } interface LoDashImplicitWrapper { @@ -6470,7 +6376,7 @@ declare namespace _ { * @see _.forEach */ each( - iteratee: ListIterator + iteratee: StringIterator ): LoDashImplicitWrapper; } @@ -6497,7 +6403,7 @@ declare namespace _ { * @see _.forEach */ each( - iteratee: ListIterator + iteratee: StringIterator ): LoDashExplicitWrapper; } @@ -6521,101 +6427,7 @@ declare namespace _ { //_.eachRight interface LoDashStatic { - /** - * @see _.forEachRight - */ - eachRight( - collection: string, - iteratee?: ListIterator - ): string; - - /** - * @see _.forEachRight - */ - eachRight( - collection: T[], - iteratee?: ListIterator - ): T[]; - - /** - * @see _.forEachRight - */ - eachRight( - collection: List, - iteratee?: ListIterator - ): List; - - /** - * @see _.forEachRight - */ - eachRight( - collection: Dictionary, - iteratee?: DictionaryIterator - ): Dictionary; - - /** - * @see _.forEachRight - */ - eachRight( - collection: T, - iteratee?: ObjectIterator - ): T; - - /** - * @see _.forEachRight - */ - eachRight( - collection: T, - iteratee?: ObjectIterator - ): T; - - /** - * @see _.forEachRight - */ - eachRight( - collection: string | null | undefined, - iteratee?: ListIterator - ): string | null | undefined; - - /** - * @see _.forEachRight - */ - eachRight( - collection: T[] | null | undefined, - iteratee?: ListIterator - ): T[] | null | undefined; - - /** - * @see _.forEachRight - */ - eachRight( - collection: List | null | undefined, - iteratee?: ListIterator - ): List | null | undefined; - - /** - * @see _.forEachRight - */ - eachRight( - collection: Dictionary | null | undefined, - iteratee?: DictionaryIterator - ): Dictionary | null | undefined; - - /** - * @see _.forEachRight - */ - eachRight( - collection: T | null | undefined, - iteratee?: ObjectIterator - ): T | null | undefined; - - /** - * @see _.forEachRight - */ - eachRight( - collection: T | null | undefined, - iteratee?: ObjectIterator - ): T | null | undefined; + eachRight: typeof _.forEachRight; } interface LoDashImplicitWrapper { @@ -6623,7 +6435,7 @@ declare namespace _ { * @see _.forEachRight */ eachRight( - iteratee: ListIterator + iteratee: StringIterator ): LoDashImplicitWrapper; } @@ -6650,7 +6462,7 @@ declare namespace _ { * @see _.forEachRight */ eachRight( - iteratee: ListIterator + iteratee: StringIterator ): LoDashExplicitWrapper; } @@ -6708,15 +6520,7 @@ declare namespace _ { */ every( collection: List|Dictionary|NumericDictionary | null | undefined, - predicate?: string|any[] - ): boolean; - - /** - * @see _.every - */ - every( - collection: List|Dictionary|NumericDictionary | null | undefined, - predicate?: PartialObject + predicate?: string|any[]|PartialObject ): boolean; } @@ -6832,11 +6636,27 @@ declare namespace _ { * @param thisArg The this binding of predicate. * @return Returns the new filtered array. */ + filter( + collection: List | null | undefined, + predicate: ListIteratorTypeGuard + ): S[]; + + /** + * @see _.filter + */ filter( collection: List | null | undefined, predicate?: ListIterator ): T[]; + /** + * @see _.filter + */ + filter( + collection: Dictionary | null | undefined, + predicate: DictionaryIteratorTypeGuard + ): S[]; + /** * @see _.filter */ @@ -6858,15 +6678,7 @@ declare namespace _ { */ filter( collection: List|Dictionary | null | undefined, - predicate: string|RegExp - ): T[]; - - /** - * @see _.filter - */ - filter( - collection: List|Dictionary | null | undefined, - predicate: PartialObject + predicate: string | [string, any] | RegExp | PartialObject ): T[]; } @@ -6883,42 +6695,32 @@ declare namespace _ { /** * @see _.filter */ - filter( - predicate: ListIterator - ): LoDashImplicitArrayWrapper; + filter( + predicate: ListIteratorTypeGuard + ): LoDashImplicitArrayWrapper; /** * @see _.filter */ filter( - predicate: string|RegExp + predicate: ListIterator | string | [string, any] | RegExp | PartialObject ): LoDashImplicitArrayWrapper; - - /** - * @see _.filter - */ - filter(predicate: PartialObject): LoDashImplicitArrayWrapper; } interface LoDashImplicitObjectWrapperBase { /** * @see _.filter */ - filter( - predicate: ListIterator|DictionaryIterator - ): LoDashImplicitArrayWrapper; + filter( + predicate: ListIteratorTypeGuard + ): LoDashImplicitArrayWrapper; /** * @see _.filter */ filter( - predicate: string|RegExp + predicate: ListIterator | DictionaryIterator | string | [string, any] | RegExp | PartialObject ): LoDashImplicitArrayWrapper; - - /** - * @see _.filter - */ - filter(predicate: PartialObject): LoDashImplicitArrayWrapper; } interface LoDashExplicitWrapper { @@ -6934,42 +6736,32 @@ declare namespace _ { /** * @see _.filter */ - filter( - predicate: ListIterator - ): LoDashExplicitArrayWrapper; + filter( + predicate: ListIteratorTypeGuard + ): LoDashExplicitArrayWrapper; /** * @see _.filter */ filter( - predicate: string|RegExp + predicate: ListIterator | string | [string, any] | RegExp | PartialObject ): LoDashExplicitArrayWrapper; - - /** - * @see _.filter - */ - filter(predicate: PartialObject): LoDashExplicitArrayWrapper; } interface LoDashExplicitObjectWrapperBase { /** * @see _.filter */ - filter( - predicate: ListIterator|DictionaryIterator - ): LoDashExplicitArrayWrapper; + filter( + predicate: ListIteratorTypeGuard + ): LoDashExplicitArrayWrapper; /** * @see _.filter */ filter( - predicate: string|RegExp + predicate: ListIterator | DictionaryIterator | string | [string, any] | RegExp | PartialObject ): LoDashExplicitArrayWrapper; - - /** - * @see _.filter - */ - filter(predicate: PartialObject): LoDashExplicitArrayWrapper; } //_.find @@ -6992,12 +6784,30 @@ declare namespace _ { * @param fromIndex The index to search from. * @return Returns the matched element, else undefined. */ + find( + collection: List | null | undefined, + predicate: ListIteratorTypeGuard, + fromIndex?: number + ): S|undefined; + + /** + * @see _.find + */ find( collection: List | null | undefined, predicate?: ListIterator, fromIndex?: number ): T|undefined; + /** + * @see _.find + */ + find( + collection: Dictionary | null | undefined, + predicate: DictionaryIteratorTypeGuard, + fromIndex?: number + ): S|undefined; + /** * @see _.find */ @@ -7012,16 +6822,7 @@ declare namespace _ { */ find( collection: List|Dictionary | null | undefined, - predicate?: string, - fromIndex?: number - ): T|undefined; - - /** - * @see _.find - */ - find( - collection: List|Dictionary | null | undefined, - predicate?: PartialObject, + predicate?: string | PartialObject | [string, any], fromIndex?: number ): T|undefined; } @@ -7030,24 +6831,16 @@ declare namespace _ { /** * @see _.find */ - find( - predicate?: ListIterator, + find( + predicate: ListIteratorTypeGuard, fromIndex?: number - ): T|undefined; + ): S|undefined; /** * @see _.find */ find( - predicate?: string, - fromIndex?: number - ): T|undefined; - - /** - * @see _.find - */ - find( - predicate?: PartialObject, + predicate?: ListIterator | string | PartialObject | [string, any], fromIndex?: number ): T|undefined; } @@ -7057,23 +6850,7 @@ declare namespace _ { * @see _.find */ find( - predicate?: ListIterator|DictionaryIterator, - fromIndex?: number - ): TResult|undefined; - - /** - * @see _.find - */ - find( - predicate?: string, - fromIndex?: number - ): TResult|undefined; - - /** - * @see _.find - */ - find( - predicate?: PartialObject, + predicate?: ListIterator | DictionaryIterator | string | PartialObject | [string, any], fromIndex?: number ): TResult|undefined; } @@ -7083,23 +6860,7 @@ declare namespace _ { * @see _.find */ find( - predicate?: ListIterator, - fromIndex?: number - ): any; - - /** - * @see _.find - */ - find( - predicate?: string, - fromIndex?: number - ): any; - - /** - * @see _.find - */ - find( - predicate?: PartialObject, + predicate?: ListIterator | string | PartialObject | [string, any], fromIndex?: number ): any; } @@ -7110,144 +6871,87 @@ declare namespace _ { * This method is like _.find except that it iterates over elements of a collection from * right to left. * @param collection Searches for a value in this list. - * @param callback The function called per iteration. + * @param predicate The function called per iteration. * @param fromIndex The index to search from. * @return The found element, else undefined. **/ - findLast( - collection: T[] | null | undefined, - callback: ListIterator, + findLast( + collection: List | null | undefined, + predicate: ListIteratorTypeGuard, fromIndex?: number - ): T|undefined; - + ): S|undefined; + /** - * @see _.find - **/ + * @see _.findLast + */ findLast( collection: List | null | undefined, - callback: ListIterator, + predicate?: ListIterator, fromIndex?: number ): T|undefined; /** - * @see _.find - **/ + * @see _.findLast + */ + findLast( + collection: Dictionary | null | undefined, + predicate: DictionaryIteratorTypeGuard, + fromIndex?: number + ): S|undefined; + + /** + * @see _.findLast + */ findLast( collection: Dictionary | null | undefined, - callback: DictionaryIterator, + predicate?: DictionaryIterator, fromIndex?: number ): T|undefined; /** - * @see _.find - * @param _.pluck style callback - **/ - findLast( - collection: T[] | null | undefined, - whereValue: W, - fromIndex?: number - ): T|undefined; - - /** - * @see _.find - * @param _.pluck style callback - **/ - findLast( - collection: List | null | undefined, - whereValue: W, - fromIndex?: number - ): T|undefined; - - /** - * @see _.find - * @param _.pluck style callback - **/ - findLast( - collection: Dictionary | null | undefined, - whereValue: W, - fromIndex?: number - ): T|undefined; - - /** - * @see _.find - * @param _.where style callback - **/ + * @see _.findLast + */ findLast( - collection: T[] | null | undefined, - pluckValue: string, - fromIndex?: number - ): T|undefined; - - /** - * @see _.find - * @param _.where style callback - **/ - findLast( - collection: List | null | undefined, - pluckValue: string, - fromIndex?: number - ): T|undefined; - - /** - * @see _.find - * @param _.where style callback - **/ - findLast( - collection: Dictionary | null | undefined, - pluckValue: string, + collection: List|Dictionary | null | undefined, + predicate?: string | PartialObject | [string, any], fromIndex?: number ): T|undefined; } interface LoDashImplicitArrayWrapperBase { /** - * @see _.findLast - */ - findLast( - callback: ListIterator, + * @see _.findLast + */ + findLast( + predicate: ListIteratorTypeGuard, fromIndex?: number - ): T|undefined; - /** - * @see _.findLast - * @param _.where style callback - */ - findLast( - whereValue: W, - fromIndex?: number - ): T|undefined; + ): S|undefined; /** - * @see _.findLast - * @param _.where style callback - */ + * @see _.findLast + */ findLast( - pluckValue: string, + predicate?: ListIterator | string | PartialObject | [string, any], fromIndex?: number ): T|undefined; } + interface LoDashImplicitObjectWrapperBase { + /** + * @see _.findLast + */ + findLast( + predicate?: ListIterator | DictionaryIterator | string | PartialObject | [string, any], + fromIndex?: number + ): TResult|undefined; + } + interface LoDashExplicitWrapperBase { /** * @see _.findLast */ findLast( - predicate?: ListIterator, - fromIndex?: number - ): any; - - /** - * @see _.findLast - */ - findLast( - predicate?: string, - fromIndex?: number - ): any; - - /** - * @see _.findLast - */ - findLast( - predicate?: PartialObject, + predicate?: ListIterator | string | PartialObject | [string, any], fromIndex?: number ): any; } @@ -7263,17 +6967,16 @@ declare namespace _ { * @param iteratee The function invoked per iteration. * @return Returns the new flattened array. */ - flatMap( - collection: List | null | undefined, - iteratee?: ListIterator> - ): TResult[]; + flatMap( + collection: List> | Dictionary> | NumericDictionary> | null | undefined + ): T[]; /** * @see _.flatMap */ - flatMap( - collection: List | null | undefined, - iteratee?: ListIterator> + flatMap( + collection: List | null | undefined, + iteratee: ListIterator> | string ): TResult[]; /** @@ -7281,15 +6984,7 @@ declare namespace _ { */ flatMap( collection: Dictionary | null | undefined, - iteratee?: DictionaryIterator> - ): TResult[]; - - /** - * @see _.flatMap - */ - flatMap( - collection: Dictionary | null | undefined, - iteratee?: DictionaryIterator> + iteratee: DictionaryIterator> | string ): TResult[]; /** @@ -7297,71 +6992,24 @@ declare namespace _ { */ flatMap( collection: NumericDictionary | null | undefined, - iteratee?: NumericDictionaryIterator> + iteratee: NumericDictionaryIterator> | string ): TResult[]; /** * @see _.flatMap */ flatMap( - collection: NumericDictionary | null | undefined, - iteratee?: NumericDictionaryIterator> + collection: object | null | undefined, + iteratee?: ObjectIterator> | string ): TResult[]; /** * @see _.flatMap */ - flatMap( - collection: TObject | null | undefined, - iteratee?: ObjectIterator> - ): TResult[]; - - /** - * @see _.flatMap - */ - flatMap( - collection: Object | null | undefined, - iteratee?: ObjectIterator> - ): TResult[]; - - /** - * @see _.flatMap - */ - flatMap( - collection: TObject | null | undefined, - iteratee: TWhere + flatMap( + collection: object | null | undefined, + iteratee: object ): boolean[]; - - /** - * @see _.flatMap - */ - flatMap( - collection: TObject | null | undefined, - iteratee: Object|string - ): TResult[]; - - /** - * @see _.flatMap - */ - flatMap( - collection: TObject | null | undefined, - iteratee: [string, any] - ): boolean[]; - - /** - * @see _.flatMap - */ - flatMap( - collection: string | null | undefined - ): string[]; - - /** - * @see _.flatMap - */ - flatMap( - collection: Object | null | undefined, - iteratee?: Object|string - ): TResult[]; } interface LoDashImplicitWrapper { @@ -7369,7 +7017,7 @@ declare namespace _ { * @see _.flatMap */ flatMap( - iteratee: ListIterator> + iteratee: StringIterator> ): LoDashImplicitArrayWrapper; /** @@ -7386,18 +7034,11 @@ declare namespace _ { iteratee: ListIterator>|string ): LoDashImplicitArrayWrapper; - /** - * @see _.flatMap - */ - flatMap( - iteratee: TWhere - ): LoDashImplicitArrayWrapper; - /** * @see _.flatMap */ flatMap( - iteratee: [string, any] + iteratee: object ): LoDashImplicitArrayWrapper; /** @@ -7421,18 +7062,11 @@ declare namespace _ { iteratee: ObjectIterator>|string ): LoDashImplicitArrayWrapper; - /** - * @see _.flatMap - */ - flatMap( - iteratee: TWhere - ): LoDashImplicitArrayWrapper; - /** * @see _.flatMap */ flatMap( - iteratee: [string, any] + iteratee: object ): LoDashImplicitArrayWrapper; /** @@ -7446,7 +7080,7 @@ declare namespace _ { * @see _.flatMap */ flatMap( - iteratee: ListIterator> + iteratee: StringIterator> ): LoDashExplicitArrayWrapper; /** @@ -7463,18 +7097,11 @@ declare namespace _ { iteratee: ListIterator>|string ): LoDashExplicitArrayWrapper; - /** - * @see _.flatMap - */ - flatMap( - iteratee: TWhere - ): LoDashExplicitArrayWrapper; - /** * @see _.flatMap */ flatMap( - iteratee: [string, any] + iteratee: object ): LoDashExplicitArrayWrapper; /** @@ -7498,18 +7125,11 @@ declare namespace _ { iteratee: ObjectIterator>|string ): LoDashExplicitArrayWrapper; - /** - * @see _.flatMap - */ - flatMap( - iteratee: TWhere - ): LoDashExplicitArrayWrapper; - /** * @see _.flatMap */ flatMap( - iteratee: [string, any] + iteratee: object ): LoDashExplicitArrayWrapper; /** @@ -7534,98 +7154,34 @@ declare namespace _ { * @param iteratee The function invoked per iteration. * @param thisArg The this binding of iteratee. */ - forEach( - collection: T[], + forEach( + collection: TString, + iteratee?: StringIterator + ): TString; + + /** + * @see _.forEach + */ + forEach | null | undefined>( + collection: TList & (List | null | undefined), iteratee?: ListIterator - ): T[]; + ): TList; /** * @see _.forEach */ - forEach( - collection: string, - iteratee?: ListIterator - ): string; - - /** - * @see _.forEach - */ - forEach( - collection: List, - iteratee?: ListIterator - ): List; - - /** - * @see _.forEach - */ - forEach( - collection: Dictionary, + forEach | null | undefined>( + collection: TDictionary & (Dictionary | null | undefined), iteratee?: DictionaryIterator - ): Dictionary; + ): TDictionary; /** * @see _.forEach */ - forEach( + forEach( collection: T, iteratee?: ObjectIterator ): T; - - /** - * @see _.forEach - */ - forEach( - collection: T, - iteratee?: ObjectIterator - ): T; - - /** - * @see _.forEach - */ - forEach( - collection: T[] | null | undefined, - iteratee?: ListIterator - ): T[] | null | undefined; - - /** - * @see _.forEach - */ - forEach( - collection: string | null | undefined, - iteratee?: ListIterator - ): string | null | undefined; - - /** - * @see _.forEach - */ - forEach( - collection: List | null | undefined, - iteratee?: ListIterator - ): List | null | undefined; - - /** - * @see _.forEach - */ - forEach( - collection: Dictionary | null | undefined, - iteratee?: DictionaryIterator - ): Dictionary | null | undefined; - - /** - * @see _.forEach - */ - forEach( - collection: T | null | undefined, - iteratee?: ObjectIterator - ): T | null | undefined; - - /** - * @see _.forEach - */ - forEach( - collection: T | null | undefined, - iteratee?: ObjectIterator - ): T | null | undefined; } interface LoDashImplicitWrapper { @@ -7633,7 +7189,7 @@ declare namespace _ { * @see _.forEach */ forEach( - iteratee: ListIterator + iteratee: StringIterator ): LoDashImplicitWrapper; } @@ -7660,7 +7216,7 @@ declare namespace _ { * @see _.forEach */ forEach( - iteratee: ListIterator + iteratee: StringIterator ): LoDashExplicitWrapper; } @@ -7693,98 +7249,34 @@ declare namespace _ { * @param iteratee The function called per iteration. * @param thisArg The this binding of callback. */ - forEachRight( - collection: T[], + forEachRight( + collection: TString, + iteratee?: StringIterator + ): TString; + + /** + * @see _.forEachRight + */ + forEachRight | null | undefined>( + collection: TList & (List | null | undefined), iteratee?: ListIterator - ): T[]; + ): TList; /** * @see _.forEachRight */ - forEachRight( - collection: string, - iteratee?: ListIterator - ): string; - - /** - * @see _.forEachRight - */ - forEachRight( - collection: List, - iteratee?: ListIterator - ): List; - - /** - * @see _.forEachRight - */ - forEachRight( - collection: Dictionary, + forEachRight | null | undefined>( + collection: TDictionary & (Dictionary | null | undefined), iteratee?: DictionaryIterator - ): Dictionary; + ): TDictionary; /** * @see _.forEachRight */ - forEachRight( + forEachRight( collection: T, iteratee?: ObjectIterator ): T; - - /** - * @see _.forEachRight - */ - forEachRight( - collection: T, - iteratee?: ObjectIterator - ): T; - - /** - * @see _.forEachRight - */ - forEachRight( - collection: T[] | null | undefined, - iteratee?: ListIterator - ): T[] | null | undefined; - - /** - * @see _.forEachRight - */ - forEachRight( - collection: string | null | undefined, - iteratee?: ListIterator - ): string | null | undefined; - - /** - * @see _.forEachRight - */ - forEachRight( - collection: List | null | undefined, - iteratee?: ListIterator - ): List | null | undefined; - - /** - * @see _.forEachRight - */ - forEachRight( - collection: Dictionary | null | undefined, - iteratee?: DictionaryIterator - ): Dictionary | null | undefined; - - /** - * @see _.forEachRight - */ - forEachRight( - collection: T | null | undefined, - iteratee?: ObjectIterator - ): T | null | undefined; - - /** - * @see _.forEachRight - */ - forEachRight( - collection: T | null | undefined, - iteratee?: ObjectIterator - ): T | null | undefined; } interface LoDashImplicitWrapper { @@ -7792,7 +7284,7 @@ declare namespace _ { * @see _.forEachRight */ forEachRight( - iteratee: ListIterator + iteratee: StringIterator ): LoDashImplicitWrapper; } @@ -7819,7 +7311,7 @@ declare namespace _ { * @see _.forEachRight */ forEachRight( - iteratee: ListIterator + iteratee: StringIterator ): LoDashExplicitWrapper; } @@ -13839,7 +13331,12 @@ declare namespace _ { */ meanBy( collection: List | null | undefined, - iteratee?: DictionaryIterator + iteratee?: ListIterator | string + ): number; + + meanBy( + collection: Dictionary | null | undefined, + iteratee?: DictionaryIterator | string ): number; } @@ -18995,9 +18492,9 @@ declare namespace _ { * _.filter(users, 'age > 36'); * // => [{ 'user': 'fred', 'age': 40 }] */ - iteratee( - func: Function - ): (...args: any[]) => TResult; + iteratee( + func: TFunction + ): TFunction; /** * @see _.iteratee @@ -19963,8 +19460,12 @@ declare namespace _ { type ListIterator = (value: T, index: number, collection: List) => TResult; + type ListIteratorTypeGuard = (value: T, index: number, collection: List) => value is S; + type DictionaryIterator = (value: T, key: string, collection: Dictionary) => TResult; + type DictionaryIteratorTypeGuard = (value: T, key: string, collection: Dictionary) => value is S; + type NumericDictionaryIterator = (value: T, key: number, collection: Dictionary) => TResult; type ObjectIterator = (element: T, key: string, collection: any) => TResult; diff --git a/types/lodash/lodash-tests.ts b/types/lodash/lodash-tests.ts index b772da563b..46e615d4ae 100644 --- a/types/lodash/lodash-tests.ts +++ b/types/lodash/lodash-tests.ts @@ -722,6 +722,7 @@ namespace TestFindIndex { result = _.findIndex(list, ''); result = _.findIndex<{a: number}, TResult>(list, {a: 42}); result = _.findIndex(list, predicateFn, fromIndex); + result = _.findIndex([{ b: 5 }], ['b', 5]); result = _(array).findIndex(); result = _(array).findIndex(predicateFn); @@ -775,6 +776,7 @@ namespace TestFindLastIndex { result = _.findLastIndex(list, ''); result = _.findLastIndex<{a: number}, TResult>(list, {a: 42}); result = _.findLastIndex(list, predicateFn, fromIndex); + result = _.findLastIndex([{ b: 5 }], ['b', 5]); result = _(array).findLastIndex(); result = _(array).findLastIndex(predicateFn); @@ -3449,8 +3451,7 @@ namespace TestEach { let dictionary: _.Dictionary = {}; let nilArray: TResult[] | null | undefined = [] as any; let nilList: _.List | null | undefined = [] as any; - let obj: any = {}; - let nilDictionary: _.Dictionary | null | undefined = obj; + let nilDictionary: _.Dictionary | null | undefined = any; let stringIterator: (char: string, index: number, string: string) => any = (char: string, index: number, string: string) => 1; let listIterator: (value: TResult, index: number, collection: _.List) => any = (value: TResult, index: number, collection: _.List) => 1; @@ -3471,37 +3472,37 @@ namespace TestEach { { let result: TResult[]; - result = _.each(array, listIterator); + result = _.each(array, listIterator); } { let result: TResult[] | null | undefined; - result = _.each(nilArray, listIterator); + result = _.each(nilArray, listIterator); } { let result: _.List; - result = _.each(list, listIterator); + result = _.each(list, listIterator); } { let result: _.List | null | undefined; - result = _.each(nilList, listIterator); + result = _.each(nilList, listIterator); } { let result: _.Dictionary; - result = _.each(dictionary, dictionaryIterator); + result = _.each(dictionary, dictionaryIterator); } { let result: _.Dictionary | null | undefined; - result = _.each(nilDictionary, dictionaryIterator); + result = _.each(nilDictionary, dictionaryIterator); } { @@ -3560,8 +3561,7 @@ namespace TestEachRight { let dictionary: _.Dictionary = {}; let nilArray: TResult[] | null | undefined = [] as any; let nilList: _.List | null | undefined = [] as any; - let obj: any = {}; - let nilDictionary: _.Dictionary | null | undefined = obj; + let nilDictionary: _.Dictionary | null | undefined = any; let stringIterator: (char: string, index: number, string: string) => any = (char: string, index: number, string: string) => 1; let listIterator: (value: TResult, index: number, collection: _.List) => any = (value: TResult, index: number, collection: _.List) => 1; @@ -3582,37 +3582,37 @@ namespace TestEachRight { { let result: TResult[]; - result = _.eachRight(array, listIterator); + result = _.eachRight(array, listIterator); } { let result: TResult[] | null | undefined; - result = _.eachRight(nilArray, listIterator); + result = _.eachRight(nilArray, listIterator); } { let result: _.List; - result = _.eachRight(list, listIterator); + result = _.eachRight(list, listIterator); } { let result: _.List | null | undefined; - result = _.eachRight(nilList, listIterator); + result = _.eachRight(nilList, listIterator); } { let result: _.Dictionary; - result = _.eachRight(dictionary, dictionaryIterator); + result = _.eachRight(dictionary, dictionaryIterator); } { let result: _.Dictionary | null | undefined; - result = _.eachRight(nilDictionary, dictionaryIterator); + result = _.eachRight(nilDictionary, dictionaryIterator); } { @@ -3783,16 +3783,19 @@ namespace TestFilter { result = _.filter(array, ''); result = _.filter(array, /./); result = _.filter(array, {a: 42}); + result = _.filter(array, ["a", 42]); result = _.filter(list, listIterator); result = _.filter(list, ''); result = _.filter(list, /./); result = _.filter(list, {a: 42}); + result = _.filter(list, ["a", 42]); result = _.filter(dictionary, dictionaryIterator); result = _.filter(dictionary, ''); result = _.filter(dictionary, /./); result = _.filter(dictionary, {a: 42}); + result = _.filter(dictionary, ["a", 42]); } { @@ -3808,16 +3811,19 @@ namespace TestFilter { result = _(array).filter(''); result = _(array).filter(/./); result = _(array).filter({a: 42}); + result = _(array).filter(["a", 42]); result = _(list).filter(listIterator); result = _(list).filter(''); result = _(list).filter(/./); result = _(list).filter({a: 42}); + result = _(list).filter(["a", 42]); result = _(dictionary).filter(dictionaryIterator); result = _(dictionary).filter(''); result = _(dictionary).filter(/./); result = _(dictionary).filter({a: 42}); + result = _(dictionary).filter(["a", 42]); } { @@ -3833,16 +3839,32 @@ namespace TestFilter { result = _(array).chain().filter(''); result = _(array).chain().filter(/./); result = _(array).chain().filter({a: 42}); + result = _(array).chain().filter(["a", 42]); result = _(list).chain().filter(listIterator); result = _(list).chain().filter(''); result = _(list).chain().filter(/./); result = _(list).chain().filter({a: 42}); + result = _(list).chain().filter(["a", 42]); result = _(dictionary).chain().filter(dictionaryIterator); result = _(dictionary).chain().filter(''); result = _(dictionary).chain().filter(/./); result = _(dictionary).chain().filter({a: 42}); + result = _(dictionary).chain().filter(["a", 42]); + } + + { + // Test filtering with type guard + let a2: Array | null | undefined = any; + let d2: _.Dictionary | null | undefined = any; + + _.filter(a2, (item: string | number): item is number => typeof item === "number"); // $ExpectType number[] + _.filter(d2, (item: string | number): item is number => typeof item === "number"); // $ExpectType number[] + _(a2).filter((item: string | number): item is number => typeof item === "number"); // $ExpectType LoDashImplicitArrayWrapper + _(d2).filter((item: string | number): item is number => typeof item === "number"); // $ExpectType LoDashImplicitArrayWrapper + _(a2).chain().filter((item: string | number): item is number => typeof item === "number"); // $ExpectType LoDashExplicitArrayWrapper + _(d2).chain().filter((item: string | number): item is number => typeof item === "number"); // $ExpectType LoDashExplicitArrayWrapper } } @@ -3858,6 +3880,7 @@ namespace TestFind { let result: TResult | undefined; + result = _.find(array); result = _.find(array); result = _.find(array, listIterator); result = _.find(array, listIterator, 1); @@ -3865,7 +3888,10 @@ namespace TestFind { result = _.find(array, '', 1); result = _.find(array, {a: 42}); result = _.find(array, {a: 42}, 1); + result = _.find(array, ['a', 5]); + result = _.find(array, ['a', 5], 1); + result = _.find(list); result = _.find(list); result = _.find(list, listIterator); result = _.find(list, listIterator, 1); @@ -3873,7 +3899,10 @@ namespace TestFind { result = _.find(list, '', 1); result = _.find(list, {a: 42}); result = _.find(list, {a: 42}, 1); + result = _.find(list, ['a', 5]); + result = _.find(list, ['a', 5], 1); + result = _.find(dictionary); result = _.find(dictionary); result = _.find(dictionary, dictionaryIterator); result = _.find(dictionary, dictionaryIterator, 1); @@ -3881,6 +3910,8 @@ namespace TestFind { result = _.find(dictionary, '', 1); result = _.find(dictionary, {a: 42}); result = _.find(dictionary, {a: 42}, 1); + result = _.find(dictionary, ['a', 5]); + result = _.find(dictionary, ['a', 5], 1); result = _(array).find(); result = _(array).find(listIterator); @@ -3889,6 +3920,8 @@ namespace TestFind { result = _(array).find('', 1); result = _(array).find({a: 42}); result = _(array).find({a: 42}, 1); + result = _(array).find(['a', 5]); + result = _(array).find(['a', 5], 1); result = _(list).find(); result = _(list).find(listIterator); @@ -3897,6 +3930,8 @@ namespace TestFind { result = _(list).find('', 1); result = _(list).find({a: 42}); result = _(list).find({a: 42}, 1); + result = _(list).find(['a', 5]); + result = _(list).find(['a', 5], 1); result = _(dictionary).find(); result = _(dictionary).find(dictionaryIterator); @@ -3905,19 +3940,91 @@ namespace TestFind { result = _(dictionary).find('', 1); result = _(dictionary).find({a: 42}); result = _(dictionary).find({a: 42}, 1); + result = _(dictionary).find(['a', 5]); + result = _(dictionary).find(['a', 5], 1); + + result = _.find([any as TResult, null, undefined], (value: TResult | null | undefined): value is TResult | undefined => value !== null); + result = _([any as TResult, null, undefined]).find((value: TResult | null | undefined): value is TResult | undefined => value !== null); } -result = _.findLast([1, 2, 3, 4], num => num % 2 == 0); -result = _.findLast(foodsCombined, { 'type': 'vegetable' }); -result = _.findLast(foodsCombined, 'organic'); +// _.findLast +namespace TestFindLast { + let array: TResult[] | null | undefined = [] as any; + let list: _.List | null | undefined = [] as any; + let obj: any = {}; + let dictionary: _.Dictionary | null | undefined = obj; -result = _.findLast(foodsCombined, 'organic', 1); + let listIterator = (value: TResult, index: number, collection: _.List) => true; + let dictionaryIterator = (value: TResult, key: string, collection: _.Dictionary) => true; -result = _([1, 2, 3, 4]).findLast(num => num % 2 == 0); -result = _(foodsCombined).findLast({ 'type': 'vegetable' }); -result = _(foodsCombined).findLast('organic'); + let result: TResult | undefined; -result = _(foodsCombined).findLast('organic', 1); + result = _.findLast(array); + result = _.findLast(array); + result = _.findLast(array, listIterator); + result = _.findLast(array, listIterator, 1); + result = _.findLast(array, ''); + result = _.findLast(array, '', 1); + result = _.findLast(array, {a: 42}); + result = _.findLast(array, {a: 42}, 1); + result = _.findLast(array, ['a', 5]); + result = _.findLast(array, ['a', 5], 1); + + result = _.findLast(list); + result = _.findLast(list); + result = _.findLast(list, listIterator); + result = _.findLast(list, listIterator, 1); + result = _.findLast(list, ''); + result = _.findLast(list, '', 1); + result = _.findLast(list, {a: 42}); + result = _.findLast(list, {a: 42}, 1); + result = _.findLast(list, ['a', 5]); + result = _.findLast(list, ['a', 5], 1); + + result = _.findLast(dictionary); + result = _.findLast(dictionary); + result = _.findLast(dictionary, dictionaryIterator); + result = _.findLast(dictionary, dictionaryIterator, 1); + result = _.findLast(dictionary, ''); + result = _.findLast(dictionary, '', 1); + result = _.findLast(dictionary, {a: 42}); + result = _.findLast(dictionary, {a: 42}, 1); + result = _.findLast(dictionary, ['a', 5]); + result = _.findLast(dictionary, ['a', 5], 1); + + result = _(array).findLast(); + result = _(array).findLast(listIterator); + result = _(array).findLast(listIterator, 1); + result = _(array).findLast(''); + result = _(array).findLast('', 1); + result = _(array).findLast({a: 42}); + result = _(array).findLast({a: 42}, 1); + result = _(array).findLast(['a', 5]); + result = _(array).findLast(['a', 5], 1); + + result = _(list).findLast(); + result = _(list).findLast(listIterator); + result = _(list).findLast(listIterator, 1); + result = _(list).findLast(''); + result = _(list).findLast('', 1); + result = _(list).findLast({a: 42}); + result = _(list).findLast({a: 42}, 1); + result = _(list).findLast(['a', 5]); + result = _(list).findLast(['a', 5], 1); + + result = _(dictionary).findLast(); + result = _(dictionary).findLast(dictionaryIterator); + result = _(dictionary).findLast(dictionaryIterator, 1); + result = _(dictionary).findLast(''); + result = _(dictionary).findLast('', 1); + result = _(dictionary).findLast({a: 42}); + result = _(dictionary).findLast({a: 42}, 1); + result = _(dictionary).findLast(['a', 5]); + result = _(dictionary).findLast(['a', 5], 1); + + result = _.findLast([any as TResult, null, undefined], (value: TResult | null | undefined): value is TResult | undefined => value !== null); + result = _([any as TResult, null, undefined]).findLast((value: TResult | null | undefined): value is TResult | undefined => value !== null); +} // _.flatMap namespace TestFlatMap { @@ -3936,92 +4043,72 @@ namespace TestFlatMap { let stringIterator: (value: string, index: number, collection: _.List) => string|string[] = (a, b, c) => ""; - let listIterator: (value: number, index: number, collection: _.List) => number|number[] = (a, b, c) => 1; + let listIterator: (value: number|number[], index: number, collection: _.List) => number|number[] = (a, b, c) => 1; - let dictionaryIterator: (value: number, key: number, collection: _.Dictionary) => number|number[] = (a, b, c) => 1; + let dictionaryIterator: (value: number|number[], key: string, collection: _.Dictionary) => number|number[] = (a, b, c) => 1; - let numericDictionaryIterator: (value: number, key: number, collection: _.NumericDictionary) => number|number[] = (a, b, c) => 1; + let numericDictionaryIterator: (value: number|number[], key: number, collection: _.NumericDictionary) => number|number[] = (a, b, c) => 1; { let result: string[]; result = _.flatMap('abc'); - result = _.flatMap('abc'); + result = _.flatMap('abc'); result = _.flatMap('abc', stringIterator); - result = _.flatMap('abc', stringIterator); + result = _.flatMap('abc', stringIterator); } { let result: number[]; - result = _.flatMap(numArray); + result = _.flatMap(numArray); result = _.flatMap(numArray); + result = _.flatMap(numArray, listIterator); result = _.flatMap(numArray, listIterator); - result = _.flatMap(numArray, listIterator); - result = _.flatMap<({a: number}|{a: number}[])[], number>(objArray, 'a'); result = _.flatMap(objArray, 'a'); - result = _.flatMap(numList); + result = _.flatMap(numList); result = _.flatMap(numList); + result = _.flatMap(numList, listIterator); result = _.flatMap(numList, listIterator); - result = _.flatMap(numList, listIterator); - result = _.flatMap<_.List<{a: number}|{a: number}[]>, number>(objList, 'a'); result = _.flatMap(objList, 'a'); - result = _.flatMap(numDictionary); + result = _.flatMap(numDictionary); result = _.flatMap(numDictionary); + result = _.flatMap(numDictionary, dictionaryIterator); result = _.flatMap(numDictionary, dictionaryIterator); - result = _.flatMap(numDictionary, dictionaryIterator); - result = _.flatMap<_.Dictionary<{a: number}|{a: number}[]>, number>(objDictionary, 'a'); result = _.flatMap(objDictionary, 'a'); - result = _.flatMap(numNumericDictionary); + result = _.flatMap(numNumericDictionary); result = _.flatMap(numNumericDictionary); + result = _.flatMap(numNumericDictionary, numericDictionaryIterator); result = _.flatMap(numNumericDictionary, numericDictionaryIterator); - result = _.flatMap(numNumericDictionary, numericDictionaryIterator); result = _.flatMap<_.NumericDictionary<{a: number}|{a: number}[]>, number>(objNumericDictionary, 'a'); - result = _.flatMap(objNumericDictionary, 'a'); } { let result: boolean[]; - result = _.flatMap<({a: number}|{a: number}[])[], boolean>(objArray, ['a', 42]); - result = _.flatMap(objArray, ['a', 42]); + result = _.flatMap(objArray, ['a', 42]); + result = _.flatMap(objArray, {'a': 42}); - result = _.flatMap<{a: number}, ({a: number}|{a: number}[])[]>(objArray, {'a': 42}); - result = _.flatMap<({a: number}|{a: number}[])[], boolean>(objArray, {'a': 42}); - result = _.flatMap(objArray, {'a': 42}); + result = _.flatMap(objList, ['a', 42]); + result = _.flatMap(objList, {'a': 42}); - result = _.flatMap<_.List<{a: number}|{a: number}[]>, boolean>(objList, ['a', 42]); - result = _.flatMap(objList, ['a', 42]); + result = _.flatMap(objDictionary, ['a', 42]); + result = _.flatMap(objDictionary, {'a': 42}); - result = _.flatMap<{a: number}, _.List<{a: number}|{a: number}[]>>(objList, {'a': 42}); - result = _.flatMap<_.List<{a: number}|{a: number}[]>, boolean>(objList, {'a': 42}); - result = _.flatMap(objList, {'a': 42}); - - result = _.flatMap<_.Dictionary<{a: number}|{a: number}[]>, boolean>(objDictionary, ['a', 42]); - result = _.flatMap(objDictionary, ['a', 42]); - - result = _.flatMap<{a: number}, _.Dictionary<{a: number}|{a: number}[]>>(objDictionary, {'a': 42}); - result = _.flatMap<_.Dictionary<{a: number}|{a: number}[]>, boolean>(objDictionary, {'a': 42}); - result = _.flatMap(objDictionary, {'a': 42}); - - result = _.flatMap<_.NumericDictionary<{a: number}|{a: number}[]>, boolean>(objNumericDictionary, ['a', 42]); - result = _.flatMap(objNumericDictionary, ['a', 42]); - - result = _.flatMap<{a: number}, _.NumericDictionary<{a: number}|{a: number}[]>>(objNumericDictionary, {'a': 42}); - result = _.flatMap<_.NumericDictionary<{a: number}|{a: number}[]>, boolean>(objNumericDictionary, {'a': 42}); - result = _.flatMap(objNumericDictionary, {'a': 42}); + result = _.flatMap(objNumericDictionary, ['a', 42]); + result = _.flatMap(objNumericDictionary, {'a': 42}); } { @@ -4055,16 +4142,16 @@ namespace TestFlatMap { let result: _.LoDashImplicitArrayWrapper; result = _(objArray).flatMap(['a', 42]); - result = _(objArray).flatMap<{a: number}>({a: 42}); + result = _(objArray).flatMap({a: 42}); result = _(objList).flatMap(['a', 42]); - result = _(objList).flatMap<{a: number}>({a: 42}); + result = _(objList).flatMap({a: 42}); result = _(objDictionary).flatMap(['a', 42]); - result = _(objDictionary).flatMap<{a: number}>({a: 42}); + result = _(objDictionary).flatMap({a: 42}); result = _(objNumericDictionary).flatMap(['a', 42]); - result = _(objNumericDictionary).flatMap<{a: number}>({a: 42}); + result = _(objNumericDictionary).flatMap({a: 42}); } { @@ -4098,16 +4185,16 @@ namespace TestFlatMap { let result: _.LoDashExplicitArrayWrapper; result = _(objArray).chain().flatMap(['a', 42]); - result = _(objArray).chain().flatMap<{a: number}>({a: 42}); + result = _(objArray).chain().flatMap({a: 42}); result = _(objList).chain().flatMap(['a', 42]); - result = _(objList).chain().flatMap<{a: number}>({a: 42}); + result = _(objList).chain().flatMap({a: 42}); result = _(objDictionary).chain().flatMap(['a', 42]); - result = _(objDictionary).chain().flatMap<{a: number}>({a: 42}); + result = _(objDictionary).chain().flatMap({a: 42}); result = _(objNumericDictionary).chain().flatMap(['a', 42]); - result = _(objNumericDictionary).chain().flatMap<{a: number}>({a: 42}); + result = _(objNumericDictionary).chain().flatMap({a: 42}); } } @@ -4118,77 +4205,137 @@ namespace TestForEach { let dictionary: _.Dictionary = {}; let nilArray: TResult[] | null | undefined = [] as any; let nilList: _.List | null | undefined = [] as any; - let obj: any = {}; - let nilDictionary: _.Dictionary | null | undefined = obj; + let nilDictionary: _.Dictionary | null | undefined = any; - let stringIterator: (char: string, index: number, string: string) => any = (char: string, index: number, string: string) => 1; - let listIterator: (value: TResult, index: number, collection: _.List) => any = (value: TResult, index: number, collection: _.List) => 1; - let dictionaryIterator: (value: TResult, key: string, collection: _.Dictionary) => any = (value: TResult, key: string, collection: _.Dictionary) => 1; + let listIterator: (value: TResult, index: number, collection: _.List) => any = (value, index, collection) => 1; + let dictionaryIterator: (value: TResult, key: string, collection: _.Dictionary) => any = (value, key, collection) => 1; + let objectIterator: (value: number | string | boolean, key: string, collection: TResult) => any = (value, key, collection) => 1; { let result: string; - result = _.forEach('', stringIterator); + result = _.forEach('', (value, index, collection) => { + value; // $ExpectType string + index; // $ExpectType number + collection; // $ExpectType string + }); } { let result: string | null | undefined; - result = _.forEach('' as (string | null | undefined), stringIterator); + result = _.forEach('' as (string | null | undefined), (value, index, collection) => { + value; // $ExpectType string + index; // $ExpectType number + collection; // $ExpectType string + }); } { let result: TResult[]; - - result = _.forEach(array, listIterator); + result = _.forEach(array, (value, index, collection: TResult[]) => { + value; // $ExpectType TResult + index; // $ExpectType number + }); + result = _.forEach(array, (value, index, collection) => { + value; // $ExpectType TResult + index; // $ExpectType number + // Note: ideally we'd like collection TResult[], but it seems the best we can get is List. + collection; // $ExpectType ArrayLike + }); } { let result: TResult[] | null | undefined; - result = _.forEach(nilArray, listIterator); + result = _.forEach(array, (value, index, collection: TResult[]) => { + value; // $ExpectType TResult + index; // $ExpectType number + }); + result = _.forEach(nilArray, (value, index, collection) => { + value; // $ExpectType TResult + index; // $ExpectType number + // Note: ideally we'd like collection TResult[], but it seems the best we can get is List. + collection; // $ExpectType ArrayLike + }); } { let result: _.List; - result = _.forEach(list, listIterator); + result = _.forEach(list, (value, index, collection) => { + value; // $ExpectType TResult + index; // $ExpectType number + collection; // $ExpectType ArrayLike + }); } { let result: _.List | null | undefined; - result = _.forEach(nilList, listIterator); + result = _.forEach(nilList, (value, index, collection) => { + value; // $ExpectType TResult + index; // $ExpectType number + collection; // $ExpectType ArrayLike + }); } { - let result: _.Dictionary; + let result: _.Dictionary; - result = _.forEach(dictionary, dictionaryIterator); + result = _.forEach(dictionary, (value, index, collection) => { + value; // $ExpectType TResult + index; // $ExpectType string + collection; // $ExpectType Dictionary + }); } { let result: _.Dictionary | null | undefined; - result = _.forEach(nilDictionary, dictionaryIterator); + result = _.forEach(nilDictionary, (value, index, collection) => { + value; // $ExpectType TResult + index; // $ExpectType string + collection; // $ExpectType Dictionary + }); + } + + { + let sample1: TResult = any; + sample1 = _.forEach(sample1, objectIterator); + + let sample2: TResult | null | undefined = any; + sample2 = _.forEach(sample2, objectIterator); } { let result: _.LoDashImplicitWrapper; - result = _('').forEach(stringIterator); + result = _('').forEach((value, index, collection) => { + value; // $ExpectType string + index; // $ExpectType number + collection; // $ExpectType string + }); } { let result: _.LoDashImplicitArrayWrapper; - result = _(array).forEach(listIterator); + result = _(array).forEach((value, index, collection) => { + value; // $ExpectType TResult + index; // $ExpectType number + collection; // $ExpectType ArrayLike + }); } { let result: _.LoDashImplicitNillableArrayWrapper; - result = _(nilArray).forEach(listIterator); + result = _(nilArray).forEach((value, index, collection) => { + value; // $ExpectType TResult + index; // $ExpectType number + collection; // $ExpectType ArrayLike + }); } { @@ -4218,19 +4365,31 @@ namespace TestForEach { { let result: _.LoDashExplicitWrapper; - result = _('').chain().forEach(stringIterator); + result = _('').chain().forEach((value, index, collection) => { + value; // $ExpectType string + index; // $ExpectType number + collection; // $ExpectType string + }); } { let result: _.LoDashExplicitArrayWrapper; - result = _(array).chain().forEach(listIterator); + result = _(array).chain().forEach((value, index, collection) => { + value; // $ExpectType TResult + index; // $ExpectType number + collection; // $ExpectType ArrayLike + }); } { let result: _.LoDashExplicitNillableArrayWrapper; - result = _(nilArray).chain().forEach(listIterator); + result = _(nilArray).chain().forEach((value, index, collection) => { + value; // $ExpectType TResult + index; // $ExpectType number + collection; // $ExpectType ArrayLike + }); } { @@ -4265,65 +4424,99 @@ namespace TestForEachRight { let dictionary: _.Dictionary = {}; let nilArray: TResult[] | null | undefined = [] as any; let nilList: _.List | null | undefined = [] as any; - let obj: any = {}; - let nilDictionary: _.Dictionary | null | undefined = obj; + let nilDictionary: _.Dictionary | null | undefined = any; - let stringIterator: (char: string, index: number, string: string) => any = (char: string, index: number, string: string) => 1; let listIterator: (value: TResult, index: number, collection: _.List) => any = (value: TResult, index: number, collection: _.List) => 1; let dictionaryIterator: (value: TResult, key: string, collection: _.Dictionary) => any = (value: TResult, key: string, collection: _.Dictionary) => 1; { let result: string; - result = _.forEachRight('', stringIterator); + result = _.forEachRight('', (value, index, collection) => { + value; // $ExpectType string + index; // $ExpectType number + collection; // $ExpectType string + }); } { let result: string | null | undefined; - result = _.forEachRight('' as (string | null | undefined), stringIterator); + result = _.forEachRight('' as (string | null | undefined), (value, index, collection) => { + value; // $ExpectType string + index; // $ExpectType number + collection; // $ExpectType string + }); } { let result: TResult[]; - result = _.forEachRight(array, listIterator); + result = _.forEachRight(array, (value, index, collection) => { + value; // $ExpectType TResult + index; // $ExpectType number + collection; // $ExpectType ArrayLike + }); } { let result: TResult[] | null | undefined; - result = _.forEachRight(nilArray, listIterator); + result = _.forEachRight(nilArray, (value, index, collection) => { + value; // $ExpectType TResult + index; // $ExpectType number + collection; // $ExpectType ArrayLike + }); } { let result: _.List; - result = _.forEachRight(list, listIterator); + result = _.forEachRight(list, (value, index, collection) => { + value; // $ExpectType TResult + index; // $ExpectType number + collection; // $ExpectType ArrayLike + }); } { let result: _.List | null | undefined; - result = _.forEachRight(nilList, listIterator); + result = _.forEachRight(nilList, (value, index, collection) => { + value; // $ExpectType TResult + index; // $ExpectType number + collection; // $ExpectType ArrayLike + }); } { - let result: _.Dictionary; + let result: _.Dictionary; - result = _.forEachRight(dictionary, dictionaryIterator); + result = _.forEachRight(dictionary, (value, index, collection) => { + value; // $ExpectType TResult + index; // $ExpectType string + collection; // $ExpectType Dictionary + }); } { let result: _.Dictionary | null | undefined; - result = _.forEachRight(nilDictionary, dictionaryIterator); + result = _.forEachRight(nilDictionary, (value, index, collection) => { + value; // $ExpectType TResult + index; // $ExpectType string + collection; // $ExpectType Dictionary + }); } { let result: _.LoDashImplicitWrapper; - result = _('').forEachRight(stringIterator); + result = _('').forEachRight((value, index, collection) => { + value; // $ExpectType string + index; // $ExpectType number + collection; // $ExpectType string + }); } { @@ -4365,25 +4558,37 @@ namespace TestForEachRight { { let result: _.LoDashExplicitWrapper; - result = _('').chain().forEachRight(stringIterator); + result = _('').chain().forEachRight((value, index, collection) => { + value; // $ExpectType string + index; // $ExpectType number + collection; // $ExpectType string + }); } { let result: _.LoDashExplicitArrayWrapper; - result = _(array).chain().forEachRight(listIterator); + result = _(array).chain().forEachRight((value, index, collection) => { + value; // $ExpectType TResult + index; // $ExpectType number + collection; // $ExpectType ArrayLike + }); } { let result: _.LoDashExplicitNillableArrayWrapper; - result = _(nilArray).chain().forEachRight(listIterator); + result = _(nilArray).chain().forEachRight((value, index, collection) => { + value; // $ExpectType TResult + index; // $ExpectType number + collection; // $ExpectType ArrayLike + }); } { let result: _.LoDashExplicitObjectWrapper<_.List>; - result = _(list).chain().forEachRight(listIterator); + result = _(list).chain().forEachRight(listIterator); } { @@ -6912,7 +7117,7 @@ namespace TestIsArray { let value: number|string[]|boolean[] = any; if (_.isArray(value)) { - value; // $ExpectType boolean[] | string[] + value; // $ExpectType string[] | boolean[] } else { value; // $ExpectType number @@ -8277,11 +8482,24 @@ namespace TestMean { let result: number; + result = _.mean(array); result = _.mean(array); result = _(array).mean(); } +// _.meanBy +namespace TestMean { + let array: TResult[] = []; + + let result: number; + + result = _.meanBy(array, (x) => x.a); + result = _.meanBy(array, 'a'); + + result = _(array).mean(); +} + // _.min namespace TestMin { let array: number[] = []; @@ -9457,6 +9675,8 @@ namespace TestFindKey { result = _.findKey<{a: number;}, {a: string;}>({a: ''}, {a: 42}); + result = _.findKey({a: { b: 5 }}, ['b', 5]); + result = _<{a: string;}>({a: ''}).findKey(); result = _<{a: string;}>({a: ''}).findKey(predicateFn); @@ -9510,6 +9730,8 @@ namespace TestFindLastKey { result = _.findLastKey<{a: number;}, {a: string;}>({a: ''}, {a: 42}); + result = _.findLastKey({a: { b: 5 }}, ['b', 5]); + result = _<{a: string;}>({a: ''}).findLastKey(); result = _<{a: string;}>({a: ''}).findLastKey(predicateFn); @@ -11970,9 +12192,9 @@ namespace TestIdentity { // _.iteratee namespace TestIteratee { { - let result: (...args: any[]) => TResult; - - result = _.iteratee(Function); + _.iteratee((...args: any[]): TResult => any); // $ExpectType (...args: any[]) => TResult + _.iteratee((a: TResult): boolean => any); // $ExpectType (a: TResult) => boolean + _.iteratee((a: TResult | undefined): a is undefined => any); // $ExpectType (a: TResult | undefined) => a is undefined } {