Skip to content

Commit 8293269

Browse files
authored
🤖 Merge PR DefinitelyTyped#74115 feat(lodash): add overloads for head(), last(), max(), min() by @hkleungai
1 parent d5a3b23 commit 8293269

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

‎types/lodash/common/array.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,7 @@ declare module "../index" {
849849
* @param array The array to query.
850850
* @return Returns the last element of array.
851851
*/
852+
last<T>(array: readonly [...unknown[], T]): T;
852853
last<T>(array: List<T> | null | undefined): T | undefined;
853854
}
854855
interface Collection<T> {

‎types/lodash/common/math.d.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ declare module "../index" {
101101
* @param array The array to iterate over.
102102
* @returns Returns the maximum value.
103103
*/
104+
max<T>(collection: readonly [T, ...T[]]): T;
104105
max<T>(collection: List<T> | null | undefined): T | undefined;
105106
}
106107
interface Collection<T> {
@@ -216,6 +217,7 @@ declare module "../index" {
216217
* @param array The array to iterate over.
217218
* @returns Returns the minimum value.
218219
*/
220+
min<T>(collection: readonly [T, ...T[]]): T;
219221
min<T>(collection: List<T> | null | undefined): T | undefined;
220222
}
221223
interface Collection<T> {

‎types/lodash/fp.d.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,10 @@ declare namespace _ {
11691169
}
11701170
type LodashFindLastKey1x1<T> = (object: object | null | undefined) => string | undefined;
11711171
type LodashFindLastKey1x2<T> = (predicate: lodash.ValueIteratee<T[keyof T]>) => string | undefined;
1172-
type LodashHead = <T>(array: lodash.List<T> | null | undefined) => T | undefined;
1172+
interface LodashHead {
1173+
<T>(array: readonly [T, ...unknown[]]): T;
1174+
<T>(array: lodash.List<T> | null | undefined): T | undefined;
1175+
}
11731176
interface LodashFlatMap {
11741177
<T, TResult>(iteratee: (value: T) => lodash.Many<TResult>): LodashFlatMap1x1<T, TResult>;
11751178
<T>(iteratee: lodash.__, collection: lodash.List<T> | null | undefined): LodashFlatMap1x2<T>;
@@ -2030,7 +2033,10 @@ declare namespace _ {
20302033
type LodashKebabCase = (string: string) => string;
20312034
type LodashKeys = (object: any) => string[];
20322035
type LodashKeysIn = (object: any) => string[];
2033-
type LodashLast = <T>(array: lodash.List<T> | null | undefined) => T | undefined;
2036+
interface LodashLast {
2037+
<T>(array: readonly [...unknown[], T]): T;
2038+
<T>(array: lodash.List<T> | null | undefined): T | undefined;
2039+
}
20342040
interface LodashLastIndexOf {
20352041
<T>(value: T): LodashLastIndexOf1x1<T>;
20362042
<T>(value: lodash.__, array: lodash.List<T> | null | undefined): LodashLastIndexOf1x2<T>;
@@ -2167,7 +2173,10 @@ declare namespace _ {
21672173
}
21682174
type LodashMatchesProperty1x1 = <T>(srcValue: T) => (value: any) => boolean;
21692175
type LodashMatchesProperty1x2 = (path: lodash.PropertyPath) => (value: any) => boolean;
2170-
type LodashMax = <T>(collection: lodash.List<T> | null | undefined) => T | undefined;
2176+
interface LodashMax {
2177+
<T>(collection: readonly [T, ...T[]]): T;
2178+
<T>(collection: lodash.List<T> | null | undefined): T | undefined;
2179+
}
21712180
interface LodashMaxBy {
21722181
<T>(iteratee: lodash.ValueIteratee<T>): LodashMaxBy1x1<T>;
21732182
<T>(iteratee: lodash.__, collection: lodash.List<T> | null | undefined): LodashMaxBy1x2<T>;
@@ -2234,7 +2243,10 @@ declare namespace _ {
22342243
type LodashMergeWith1x6<TObject, TSource> = (customizer: lodash.MergeWithCustomizer) => TObject & TSource;
22352244
type LodashMethod = (path: lodash.PropertyPath) => (object: any) => any;
22362245
type LodashMethodOf = (object: object) => (path: lodash.PropertyPath) => any;
2237-
type LodashMin = <T>(collection: lodash.List<T> | null | undefined) => T | undefined;
2246+
interface LodashMin {
2247+
<T>(collection: readonly [T, ...T[]]): T;
2248+
<T>(collection: lodash.List<T> | null | undefined): T | undefined;
2249+
}
22382250
interface LodashMinBy {
22392251
<T>(iteratee: lodash.ValueIteratee<T>): LodashMinBy1x1<T>;
22402252
<T>(iteratee: lodash.__, collection: lodash.List<T> | null | undefined): LodashMinBy1x2<T>;

‎types/lodash/lodash-tests.ts‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,14 @@ _.chain([1, 2, 3, 4]).unshift(5, 6); // $ExpectType CollectionChain<number>
580580

581581
fp.head("abc"); // $ExpectType string | undefined
582582
fp.head(list); // $ExpectType AbcObject | undefined
583+
fp.head([1, 2, 3]); // $ExpectType number
584+
fp.head([1, 2, 3] as number[]); // $ExpectType number | undefined
585+
fp.head([]); // $ExpectType undefined
586+
fp.head([] as []); // $ExpectType undefined
587+
fp.head([1, 2, 3] as const); // $ExpectType 1
588+
fp.head([1, 2, 3] as [number, number, number]); // $ExpectType number
589+
fp.head([1, 2, 3] as [...number[]]); // $ExpectType number | undefined
590+
fp.head([1, 2, 3] as [number, ...number[]]); // $ExpectType number
583591
}
584592

585593
// _.indexOf
@@ -862,6 +870,14 @@ _.chain([1, 2, 3, 4]).unshift(5, 6); // $ExpectType CollectionChain<number>
862870
{
863871
_.last("abc"); // $ExpectType string | undefined
864872
_.last(list); // $ExpectType AbcObject | undefined
873+
_.last([1, 2, 3]); // $ExpectType number
874+
_.last([1, 2, 3] as number[]); // $ExpectType number | undefined
875+
_.last([]); // $ExpectType undefined
876+
_.last([] as []); // $ExpectType undefined
877+
_.last([1, 2, 3] as const); // $ExpectType 3
878+
_.last([1, 2, 3] as [number, number, number]); // $ExpectType number
879+
_.last([1, 2, 3] as [...number[]]); // $ExpectType number | undefined
880+
_.last([1, 2, 3] as [...number[], number]); // $ExpectType number
865881

866882
_("abc").last(); // $ExpectType string | undefined
867883
_(list).last(); // $ExpectType AbcObject | undefined
@@ -871,6 +887,14 @@ _.chain([1, 2, 3, 4]).unshift(5, 6); // $ExpectType CollectionChain<number>
871887

872888
fp.last("abc"); // $ExpectType string | undefined
873889
fp.last(list); // $ExpectType AbcObject | undefined
890+
fp.last([1, 2, 3]); // $ExpectType number
891+
fp.last([1, 2, 3] as number[]); // $ExpectType number | undefined
892+
fp.last([]); // $ExpectType undefined
893+
fp.last([] as []); // $ExpectType undefined
894+
fp.last([1, 2, 3] as const); // $ExpectType 3
895+
fp.last([1, 2, 3] as [number, number, number]); // $ExpectType number
896+
fp.last([1, 2, 3] as [...number[]]); // $ExpectType number | undefined
897+
fp.last([1, 2, 3] as [...number[], number]); // $ExpectType number
874898
}
875899

876900
// _.nth
@@ -4965,14 +4989,24 @@ fp.now(); // $ExpectType number
49654989
const list: ArrayLike<string> = anything;
49664990

49674991
_.max(list); // $ExpectType string | undefined
4968-
_(list).max(); // $ExpectType string | undefined
4992+
_(list).max(); // $ExpectType string | undefined
49694993
_.chain(list).max(); // $ExpectType StringChain<string>
49704994
fp.max(list); // $ExpectType string | undefined
49714995

49724996
_.min(list); // $ExpectType string | undefined
4973-
_(list).min(); // $ExpectType string | undefined
4997+
_(list).min(); // $ExpectType string | undefined
49744998
_.chain(list).min(); // $ExpectType StringChain<string>
49754999
fp.min(list); // $ExpectType string | undefined
5000+
5001+
_.max([1, 2]); // $ExpectType number
5002+
_([1, 2]).max(); // $ExpectType number | undefined
5003+
_.chain([1, 2]).max(); // $ExpectType PrimitiveChain<number>
5004+
fp.max([1, 2]); // $ExpectType number
5005+
5006+
_.min([1, 2]); // $ExpectType number
5007+
_([1, 2]).min(); // $ExpectType number | undefined
5008+
_.chain([1, 2]).min(); // $ExpectType PrimitiveChain<number>
5009+
fp.min([1, 2]); // $ExpectType number
49765010
}
49775011

49785012
// _.maxBy

0 commit comments

Comments
 (0)