Skip to content

Commit 16bd140

Browse files
committed
Add types for ReadonlyArray methods
1 parent 200564e commit 16bd140

File tree

7 files changed

+134
-14
lines changed

7 files changed

+134
-14
lines changed

packages/core-js-types/src/base/proposals/array-filtering.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ interface Array<T> { // @type-options no-redefine
1111
filterReject(callbackFn: (value: T, index: number, target: T[]) => boolean, thisArg?: any): T[];
1212
}
1313

14+
interface ReadonlyArray<T> { // @type-options no-export
15+
/**
16+
* Removes the items that return true
17+
* @param callbackFn A function that accepts up to three arguments. The filterReject method calls the
18+
* callbackFn function one time for each element in the array.
19+
* @param thisArg If provided, it will be used as this value for each invocation of
20+
* predicate. If it is not provided, undefined is used instead.
21+
*/
22+
filterReject(callbackFn: (value: T, index: number, target: T[]) => boolean, thisArg?: any): T[];
23+
}
24+
1425
interface Int8Array { // @type-options no-export
1526
/**
1627
* Removes the items that return true

packages/core-js-types/src/base/proposals/array-find-from-last.d.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,31 @@ interface Array<T> { // @type-options no-redefine
2929
findLastIndex(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): number;
3030
}
3131

32+
interface ReadonlyArray<T> { // @type-options no-export
33+
/**
34+
* Returns the value of the last element in the array where predicate is true, and undefined
35+
* otherwise.
36+
* @param predicate findLast calls predicate once for each element of the array, in descending
37+
* order, until it finds one where predicate returns true. If such an element is found, findLast
38+
* immediately returns that element value. Otherwise, findLast returns undefined.
39+
* @param thisArg If provided, it will be used as this value for each invocation of
40+
* predicate. If it is not provided, undefined is used instead.
41+
*/
42+
findLast<S extends T>(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S | undefined;
43+
findLast(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T | undefined;
44+
45+
/**
46+
* Returns the index of the last element in the array where predicate is true, and -1
47+
* otherwise.
48+
* @param predicate findLastIndex calls predicate once for each element of the array, in descending
49+
* order, until it finds one where predicate returns true. If such an element is found,
50+
* findLastIndex immediately returns that element index. Otherwise, findLastIndex returns -1.
51+
* @param thisArg If provided, it will be used as this value for each invocation of
52+
* predicate. If it is not provided, undefined is used instead.
53+
*/
54+
findLastIndex(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): number;
55+
}
56+
3257
interface Int8Array { // @type-options no-export
3358
/**
3459
* Returns the value of the last element in the array where predicate is true, and undefined

packages/core-js-types/src/base/proposals/array-flat-map.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,23 @@ interface Array<T> { // @type-options no-redefine
2525
*/
2626
flat<A, D extends number = 1>(this: A, depth?: D): CoreJS.CoreJSFlatArray<A, D>[];
2727
}
28+
29+
interface ReadonlyArray<T> { // @type-options no-export
30+
/**
31+
* Calls a defined callback function on each element of an array. Then, flattens the result into
32+
* a new array.
33+
* This is identical to a map followed by flat with depth 1.
34+
* @param callback A function that accepts up to three arguments. The flatMap method calls the
35+
* callback function one time for each element in the array.
36+
* @param thisArg An object to which this keyword can refer in the callback function. If
37+
* thisArg is omitted, undefined is used as this value.
38+
*/
39+
flatMap<U, This = undefined>(callback: (this: This, value: T, index: number, array: T[]) => U | ReadonlyArray<U>, thisArg?: This): U[];
40+
41+
/**
42+
* Returns a new array with all sub-array elements concatenated into it recursively up to the
43+
* specified depth.
44+
* @param depth The maximum recursion depth
45+
*/
46+
flat<A, D extends number = 1>(this: A, depth?: D): CoreJS.CoreJSFlatArray<A, D>[];
47+
}

packages/core-js-types/src/base/proposals/array-includes.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ interface Array<T> { // @type-options no-redefine
1313
includes(searchElement: T, fromIndex?: number): boolean;
1414
}
1515

16+
interface ReadonlyArray<T> { // @type-options no-export
17+
/**
18+
* Determines whether an array includes a certain element, returning true or false as appropriate.
19+
* @param searchElement The element to search for.
20+
* @param fromIndex The position in this array at which to begin searching for searchElement.
21+
*/
22+
includes(searchElement: T, fromIndex?: number): boolean;
23+
}
24+
1625
interface Int8Array { // @type-options no-export
1726
/**
1827
* Determines whether an array includes a certain element, returning true or false as appropriate.

packages/core-js-types/src/base/proposals/array-unique.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ interface Array<T> { // @type-options no-redefine
1010
uniqueBy(resolver?: keyof T | ((value: T) => any)): Array<T>;
1111
}
1212

13+
interface ReadonlyArray<T> { // @type-options no-export
14+
/**
15+
* Returns a new array with unique items, determined by the resolver function or property key
16+
* @param resolver A function that resolves the value to check uniqueness against,
17+
* or a property key to compare the value from each item
18+
* @returns A new `Array` with unique items
19+
*/
20+
uniqueBy(resolver?: keyof T | ((value: T) => any)): Array<T>;
21+
}
22+
1323
interface Int8Array { // @type-options no-export
1424
/**
1525
* Returns a new array with unique items, determined by the resolver function or property key

packages/core-js-types/src/base/proposals/change-array-by-copy.d.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,51 @@ interface Array<T> { // @type-options no-redefine
4949
with(index: number, value: T): T[];
5050
}
5151

52+
interface ReadonlyArray<T> { // @type-options no-export
53+
/**
54+
* @returns A copy of an array with its elements reversed.
55+
*/
56+
toReversed(): T[];
57+
58+
/**
59+
* Returns a copy of an array with its elements sorted.
60+
* @param compareFn Function used to determine the order of the elements. It is expected to return
61+
* a negative value if the first argument is less than the second argument, zero if they're equal, and a positive
62+
* value otherwise. If omitted, the elements are sorted in ascending, UTF-16 code unit order.
63+
* ```ts
64+
* [11, 2, 22, 1].toSorted((a, b) => a - b) // [1, 2, 11, 22]
65+
* ```
66+
*/
67+
toSorted(compareFn?: (a: T, b: T) => number): T[];
68+
69+
/**
70+
* Copies an array and removes elements and, if necessary, inserts new elements in their place. Returns the copied array.
71+
* @param start The zero-based location in the array from which to start removing elements.
72+
* @param deleteCount The number of elements to remove.
73+
* @param items Elements to insert into the copied array in place of the deleted elements.
74+
* @returns The copied array.
75+
*/
76+
toSpliced(start: number, deleteCount: number, ...items: T[]): T[];
77+
/**
78+
* Copies an array and removes elements while returning the remaining elements.
79+
* @param start The zero-based location in the array from which to start removing elements.
80+
* @param deleteCount The number of elements to remove.
81+
* @returns A copy of the original array with the remaining elements.
82+
*/
83+
toSpliced(start: number, deleteCount?: number): T[];
84+
85+
/**
86+
* Copies an array, then overwrites the value at the provided index with the
87+
* given value. If the index is negative, then it replaces from the end
88+
* of the array.
89+
* @param index The index of the value to overwrite. If the index is
90+
* negative, then it replaces from the end of the array.
91+
* @param value The value to write into the copied array.
92+
* @returns The copied array with the updated value.
93+
*/
94+
with(index: number, value: T): T[];
95+
}
96+
5297
interface Int8Array { // @type-options no-export
5398
/**
5499
* @returns A copy of an array with its elements reversed.

packages/core-js-types/src/base/proposals/relative-indexing-method.d.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,111 +5,111 @@
55
// https://github.com/microsoft/TypeScript/blob/f450c1b80ce6dc7b04e81899db00534018932234/src/lib/es2022.string.d.ts
66
// License: https://github.com/microsoft/TypeScript/blob/v5.9.3/LICENSE.txt
77

8-
interface String { // @type-options no-export
8+
interface String {
99
/**
1010
* Returns a new String consisting of the single UTF-16 code unit located at the specified index.
1111
* @param index The zero-based index of the desired code unit. A negative index will count back from the last item.
1212
*/
1313
at(index: number): string | undefined;
1414
}
1515

16-
interface Array<T> { // @type-options no-export
16+
interface Array<T> {
1717
/**
1818
* Returns the item located at the specified index.
1919
* @param index The zero-based index of the desired code unit. A negative index will count back from the last item.
2020
*/
2121
at(index: number): T | undefined;
2222
}
2323

24-
interface ReadonlyArray<T> { // @type-options no-export
24+
interface ReadonlyArray<T> {
2525
/**
2626
* Returns the item located at the specified index.
2727
* @param index The zero-based index of the desired code unit. A negative index will count back from the last item.
2828
*/
2929
at(index: number): T | undefined;
3030
}
3131

32-
interface Int8Array { // @type-options no-export
32+
interface Int8Array {
3333
/**
3434
* Returns the item located at the specified index.
3535
* @param index The zero-based index of the desired code unit. A negative index will count back from the last item.
3636
*/
3737
at(index: number): number | undefined;
3838
}
3939

40-
interface Uint8Array { // @type-options no-export
40+
interface Uint8Array {
4141
/**
4242
* Returns the item located at the specified index.
4343
* @param index The zero-based index of the desired code unit. A negative index will count back from the last item.
4444
*/
4545
at(index: number): number | undefined;
4646
}
4747

48-
interface Uint8ClampedArray { // @type-options no-export
48+
interface Uint8ClampedArray {
4949
/**
5050
* Returns the item located at the specified index.
5151
* @param index The zero-based index of the desired code unit. A negative index will count back from the last item.
5252
*/
5353
at(index: number): number | undefined;
5454
}
5555

56-
interface Int16Array { // @type-options no-export
56+
interface Int16Array {
5757
/**
5858
* Returns the item located at the specified index.
5959
* @param index The zero-based index of the desired code unit. A negative index will count back from the last item.
6060
*/
6161
at(index: number): number | undefined;
6262
}
6363

64-
interface Uint16Array { // @type-options no-export
64+
interface Uint16Array {
6565
/**
6666
* Returns the item located at the specified index.
6767
* @param index The zero-based index of the desired code unit. A negative index will count back from the last item.
6868
*/
6969
at(index: number): number | undefined;
7070
}
7171

72-
interface Int32Array { // @type-options no-export
72+
interface Int32Array {
7373
/**
7474
* Returns the item located at the specified index.
7575
* @param index The zero-based index of the desired code unit. A negative index will count back from the last item.
7676
*/
7777
at(index: number): number | undefined;
7878
}
7979

80-
interface Uint32Array { // @type-options no-export
80+
interface Uint32Array {
8181
/**
8282
* Returns the item located at the specified index.
8383
* @param index The zero-based index of the desired code unit. A negative index will count back from the last item.
8484
*/
8585
at(index: number): number | undefined;
8686
}
8787

88-
interface Float32Array { // @type-options no-export
88+
interface Float32Array {
8989
/**
9090
* Returns the item located at the specified index.
9191
* @param index The zero-based index of the desired code unit. A negative index will count back from the last item.
9292
*/
9393
at(index: number): number | undefined;
9494
}
9595

96-
interface Float64Array { // @type-options no-export
96+
interface Float64Array {
9797
/**
9898
* Returns the item located at the specified index.
9999
* @param index The zero-based index of the desired code unit. A negative index will count back from the last item.
100100
*/
101101
at(index: number): number | undefined;
102102
}
103103

104-
interface BigInt64Array { // @type-options no-export
104+
interface BigInt64Array {
105105
/**
106106
* Returns the item located at the specified index.
107107
* @param index The zero-based index of the desired code unit. A negative index will count back from the last item.
108108
*/
109109
at(index: number): bigint | undefined;
110110
}
111111

112-
interface BigUint64Array { // @type-options no-export
112+
interface BigUint64Array {
113113
/**
114114
* Returns the item located at the specified index.
115115
* @param index The zero-based index of the desired code unit. A negative index will count back from the last item.

0 commit comments

Comments
 (0)