Skip to content

Commit b8294fd

Browse files
🤖 Merge PR DefinitelyTyped#72536 [@types/lodash] Make sure readonly arrays can't be modified by @DonaldDuck313
1 parent 2731617 commit b8294fd

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -342,15 +342,15 @@ declare module "../index" {
342342
/**
343343
* @see _.fill
344344
*/
345-
fill<T>(array: List<any> | null | undefined, value: T): List<T>;
345+
fill<T, AnyList extends List<any>>(array: AnyList extends readonly any[] ? never : AnyList | null | undefined, value: T): List<T>;
346346
/**
347347
* @see _.fill
348348
*/
349349
fill<T, U>(array: U[] | null | undefined, value: T, start?: number, end?: number): Array<T | U>;
350350
/**
351351
* @see _.fill
352352
*/
353-
fill<T, U>(array: List<U> | null | undefined, value: T, start?: number, end?: number): List<T | U>;
353+
fill<T, UList extends List<any>>(array: UList extends readonly any[] ? never : UList | null | undefined, value: T, start?: number, end?: number): List<T | UList[0]>;
354354
}
355355
interface Collection<T> {
356356
/**
@@ -932,7 +932,7 @@ declare module "../index" {
932932
/**
933933
* @see _.pull
934934
*/
935-
pull<T>(array: List<T>, ...values: T[]): List<T>;
935+
pull<TList extends List<any>>(array: TList extends readonly any[] ? never : TList, ...values: TList[0][]): TList;
936936
}
937937
interface Collection<T> {
938938
/**
@@ -968,7 +968,7 @@ declare module "../index" {
968968
/**
969969
* @see _.pullAll
970970
*/
971-
pullAll<T>(array: List<T>, values?: List<T>): List<T>;
971+
pullAll<TList extends List<any>>(array: TList extends readonly any[] ? never : TList, values?: List<TList[0]>): TList;
972972
}
973973
interface Collection<T> {
974974
/**
@@ -1007,15 +1007,15 @@ declare module "../index" {
10071007
/**
10081008
* @see _.pullAllBy
10091009
*/
1010-
pullAllBy<T>(array: List<T>, values?: List<T>, iteratee?: ValueIteratee<T>): List<T>;
1010+
pullAllBy<TList extends List<any>>(array: TList extends readonly any[] ? never : TList, values?: List<TList[0]>, iteratee?: ValueIteratee<TList[0]>): TList;
10111011
/**
10121012
* @see _.pullAllBy
10131013
*/
10141014
pullAllBy<T1, T2>(array: T1[], values: List<T2>, iteratee: ValueIteratee<T1 | T2>): T1[];
10151015
/**
10161016
* @see _.pullAllBy
10171017
*/
1018-
pullAllBy<T1, T2>(array: List<T1>, values: List<T2>, iteratee: ValueIteratee<T1 | T2>): List<T1>;
1018+
pullAllBy<T1List extends List<any>, T2>(array: T1List extends readonly any[] ? never : T1List, values: List<T2>, iteratee: ValueIteratee<T1List[0] | T2>): T1List;
10191019
}
10201020
interface Collection<T> {
10211021
/**
@@ -1054,15 +1054,15 @@ declare module "../index" {
10541054
/**
10551055
* @see _.pullAllWith
10561056
*/
1057-
pullAllWith<T>(array: List<T>, values?: List<T>, comparator?: Comparator<T>): List<T>;
1057+
pullAllWith<TList extends List<any>>(array: TList extends readonly any[] ? never : TList, values?: List<TList[0]>, comparator?: Comparator<TList[0]>): TList;
10581058
/**
10591059
* @see _.pullAllWith
10601060
*/
10611061
pullAllWith<T1, T2>(array: T1[], values: List<T2>, comparator: Comparator2<T1, T2>): T1[];
10621062
/**
10631063
* @see _.pullAllWith
10641064
*/
1065-
pullAllWith<T1, T2>(array: List<T1>, values: List<T2>, comparator: Comparator2<T1, T2>): List<T1>;
1065+
pullAllWith<T1List extends List<any>, T2>(array: T1List extends readonly any[] ? never : T1List, values: List<T2>, comparator: Comparator2<T1List[0], T2>): T1List;
10661066
}
10671067
interface Collection<T> {
10681068
/**
@@ -1091,7 +1091,7 @@ declare module "../index" {
10911091
/**
10921092
* @see _.pullAt
10931093
*/
1094-
pullAt<T>(array: List<T>, ...indexes: Array<Many<number>>): List<T>;
1094+
pullAt<TList extends List<any>>(array: TList extends readonly any[] ? never : TList, ...indexes: Array<Many<number>>): TList;
10951095
}
10961096
interface Collection<T> {
10971097
/**
@@ -1116,7 +1116,7 @@ declare module "../index" {
11161116
* @param predicate The function invoked per iteration.
11171117
* @return Returns the new array of removed elements.
11181118
*/
1119-
remove<T>(array: List<T>, predicate?: ListIteratee<T>): T[];
1119+
remove<TList extends List<any>>(array: TList extends readonly any[] ? never : TList, predicate?: ListIteratee<TList[0]>): TList[0][];
11201120
}
11211121
interface Collection<T> {
11221122
/**
@@ -1150,7 +1150,7 @@ declare module "../index" {
11501150
* console.log(array);
11511151
* // => [3, 2, 1]
11521152
*/
1153-
reverse<TList extends List<any>>(array: TList): TList;
1153+
reverse<TList extends List<any>>(array: TList extends readonly any[] ? never : TList): TList;
11541154
}
11551155
interface LoDashStatic {
11561156
/**

‎types/lodash/lodash-tests.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface AbcObject {
2222

2323
const abcObject: AbcObject = anything;
2424
const array: AbcObject[] | null | undefined = anything;
25+
const readonlyArray: readonly AbcObject[] = anything;
2526
const list: _.List<AbcObject> | null | undefined = anything;
2627
const dictionary: _.Dictionary<AbcObject> | null | undefined = anything;
2728
const numericDictionary: _.NumericDictionary<AbcObject> | null | undefined = anything;
@@ -392,6 +393,13 @@ _.chain([1, 2, 3, 4]).unshift(5, 6); // $ExpectType CollectionChain<number>
392393
_.fill(array, abcObject, 0); // $ExpectType AbcObject[]
393394
_.fill(array, abcObject, 0, 10); // $ExpectType AbcObject[]
394395

396+
// @ts-expect-error
397+
_.fill(readonlyArray, abcObject);
398+
// @ts-expect-error
399+
_.fill(readonlyArray, abcObject, 0);
400+
// @ts-expect-error
401+
_.fill(readonlyArray, abcObject, 0, 10);
402+
395403
_.fill(list, abcObject); // $ExpectType ArrayLike<AbcObject> || List<AbcObject>
396404
_.fill(list, abcObject, 0); // $ExpectType ArrayLike<AbcObject> || List<AbcObject>
397405
_.fill(list, abcObject, 0, 10); // $ExpectType ArrayLike<AbcObject> || List<AbcObject>
@@ -883,6 +891,12 @@ _.chain([1, 2, 3, 4]).unshift(5, 6); // $ExpectType CollectionChain<number>
883891
_.pull(array); // $ExpectType AbcObject[]
884892
_.pull(array, abcObject); // $ExpectType AbcObject[]
885893
_.pull(array, abcObject, abcObject, abcObject); // $ExpectType AbcObject[]
894+
// @ts-expect-error
895+
_.pull(readonlyArray);
896+
// @ts-expect-error
897+
_.pull(readonlyArray, abcObject);
898+
// @ts-expect-error
899+
_.pull(readonlyArray, abcObject, abcObject, abcObject);
886900
_.pull(list); // $ExpectType ArrayLike<AbcObject> || List<AbcObject>
887901
_.pull(list, abcObject); // $ExpectType ArrayLike<AbcObject> || List<AbcObject>
888902
_.pull(list, abcObject, abcObject, abcObject); // $ExpectType ArrayLike<AbcObject> || List<AbcObject>
@@ -913,6 +927,12 @@ _.chain([1, 2, 3, 4]).unshift(5, 6); // $ExpectType CollectionChain<number>
913927
_.pullAt(array); // $ExpectType AbcObject[]
914928
_.pullAt(array, 1); // $ExpectType AbcObject[]
915929
_.pullAt(array, [2, 3], 4); // $ExpectType AbcObject[]
930+
// @ts-expect-error
931+
_.pullAt(readonlyArray);
932+
// @ts-expect-error
933+
_.pullAt(readonlyArray, 1);
934+
// @ts-expect-error
935+
_.pullAt(readonlyArray, [2, 3], 4);
916936
_.pullAt(list); // $ExpectType ArrayLike<AbcObject> || List<AbcObject>
917937
_.pullAt(list, 1); // $ExpectType ArrayLike<AbcObject> || List<AbcObject>
918938
_.pullAt(list, [2, 3], 4); // $ExpectType ArrayLike<AbcObject> || List<AbcObject>
@@ -946,6 +966,10 @@ _.chain([1, 2, 3, 4]).unshift(5, 6); // $ExpectType CollectionChain<number>
946966

947967
_.pullAll(array); // $ExpectType AbcObject[]
948968
_.pullAll(array, values); // $ExpectType AbcObject[]
969+
// @ts-expect-error
970+
_.pullAll(readonlyArray);
971+
// @ts-expect-error
972+
_.pullAll(readonlyArray, values);
949973
_.pullAll(list); // $ExpectType ArrayLike<AbcObject> || List<AbcObject>
950974
_.pullAll(list, values); // $ExpectType ArrayLike<AbcObject> || List<AbcObject>
951975

@@ -979,6 +1003,11 @@ _.chain([1, 2, 3, 4]).unshift(5, 6); // $ExpectType CollectionChain<number>
9791003
return [];
9801004
});
9811005

1006+
// @ts-expect-error
1007+
_.pullAllBy(readonlyArray);
1008+
// @ts-expect-error
1009+
_.pullAllBy(readonlyArray, values, "a");
1010+
9821011
_.pullAllBy(list); // $ExpectType ArrayLike<AbcObject> || List<AbcObject>
9831012
_.pullAllBy(list, values); // $ExpectType ArrayLike<AbcObject> || List<AbcObject>
9841013
_.pullAllBy(list, values, "a"); // $ExpectType ArrayLike<AbcObject> || List<AbcObject>
@@ -1072,6 +1101,10 @@ _.chain([1, 2, 3, 4]).unshift(5, 6); // $ExpectType CollectionChain<number>
10721101
b; // $ExpectType AbcObject
10731102
return true;
10741103
});
1104+
// @ts-expect-error
1105+
_.pullAllWith(readonlyArray);
1106+
// @ts-expect-error
1107+
_.pullAllWith(readonlyArray, values);
10751108
_.pullAllWith(list); // $ExpectType ArrayLike<AbcObject> || List<AbcObject>
10761109
// $ExpectType ArrayLike<AbcObject> || List<AbcObject>
10771110
_.pullAllWith(list, values, (a, b) => {
@@ -1163,6 +1196,15 @@ _.chain([1, 2, 3, 4]).unshift(5, 6); // $ExpectType CollectionChain<number>
11631196
fp.remove(valueIterator)(list); // $ExpectType AbcObject[]
11641197
fp.remove("", list); // $ExpectType AbcObject[]
11651198
fp.remove({ a: 42 }, list); // $ExpectType AbcObject[]
1199+
1200+
// @ts-expect-error
1201+
_.remove(readonlyArray);
1202+
// @ts-expect-error
1203+
_.remove(readonlyArray, listIterator);
1204+
// @ts-expect-error
1205+
_.remove(readonlyArray, "");
1206+
// @ts-expect-error
1207+
_.remove(readonlyArray, { a: 42 });
11661208
}
11671209

11681210
// _.tail

0 commit comments

Comments
 (0)