Skip to content

Commit d08662b

Browse files
committed
feat: enhance Array and ReadonlyArray interfaces
1 parent 74e64af commit d08662b

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/types/global-types.d.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* eslint-disable @typescript-eslint/method-signature-style */
22

3+
import type { NonFalsy } from "./utils/non-falsy";
4+
import type { WidenLiteral } from "./utils/widen-literal";
35
import type * as Types from "@goodbyenjn/utils/types";
46

57
declare global {
@@ -50,6 +52,62 @@ declare global {
5052
}
5153
}
5254

55+
declare global {
56+
interface Array<T> {
57+
/**
58+
* Returns the elements of an array that meet the condition specified in a callback function.
59+
* @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
60+
* @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
61+
*/
62+
filter<S extends T>(predicate: BooleanConstructor, thisArg?: any): NonFalsy<NoInfer<S>>[];
63+
/**
64+
* Determines whether an array includes a certain element, returning true or false as appropriate.
65+
* @param searchElement The element to search for.
66+
* @param fromIndex The position in this array at which to begin searching for searchElement.
67+
*/
68+
includes(searchElement: T | (WidenLiteral<T> & {}), fromIndex?: number): boolean;
69+
/**
70+
* Returns the index of the first occurrence of a value in an array, or -1 if it is not present.
71+
* @param searchElement The value to locate in the array.
72+
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
73+
*/
74+
indexOf(searchElement: T | (WidenLiteral<T> & {}), fromIndex?: number): number;
75+
/**
76+
* Returns the index of the last occurrence of a specified value in an array, or -1 if it is not present.
77+
* @param searchElement The value to locate in the array.
78+
* @param fromIndex The array index at which to begin searching backward. If fromIndex is omitted, the search starts at the last index in the array.
79+
*/
80+
lastIndexOf(searchElement: T | (WidenLiteral<T> & {}), fromIndex?: number): number;
81+
}
82+
83+
interface ReadonlyArray<T> {
84+
/**
85+
* Returns the elements of an array that meet the condition specified in a callback function.
86+
* @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
87+
* @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
88+
*/
89+
filter<S extends T>(predicate: BooleanConstructor, thisArg?: any): NonFalsy<NoInfer<S>>[];
90+
/**
91+
* Determines whether an array includes a certain element, returning true or false as appropriate.
92+
* @param searchElement The element to search for.
93+
* @param fromIndex The position in this array at which to begin searching for searchElement.
94+
*/
95+
includes(searchElement: T | (WidenLiteral<T> & {}), fromIndex?: number): boolean;
96+
/**
97+
* Returns the index of the first occurrence of a value in an array.
98+
* @param searchElement The value to locate in the array.
99+
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
100+
*/
101+
indexOf(searchElement: T | (WidenLiteral<T> & {}), fromIndex?: number): number;
102+
/**
103+
* Returns the index of the last occurrence of a specified value in an array.
104+
* @param searchElement The value to locate in the array.
105+
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at the last index in the array.
106+
*/
107+
lastIndexOf(searchElement: T | (WidenLiteral<T> & {}), fromIndex?: number): number;
108+
}
109+
}
110+
53111
declare global {
54112
type AsyncFn<Return = any, Args extends readonly any[] = any[]> = Types.AsyncFn<Return, Args>;
55113
type AsyncFnWithThis<

src/types/utils/non-falsy.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type NonFalsy<T> = T extends false | 0 | "" | null | undefined | 0n ? never : T;

src/types/utils/widen-literal.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export type WidenLiteral<T> = T extends string
2+
? string
3+
: T extends number
4+
? number
5+
: T extends boolean
6+
? boolean
7+
: T extends bigint
8+
? bigint
9+
: T extends symbol
10+
? symbol
11+
: T;

0 commit comments

Comments
 (0)