Skip to content

Commit 9411d8b

Browse files
committed
♻️ Update
1 parent 65ba6f6 commit 9411d8b

File tree

4 files changed

+1901
-9
lines changed

4 files changed

+1901
-9
lines changed

src/language/average.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function average(array: number[]) {
2+
return array.reduce((a, b) => a + b) / array.length;
3+
}

src/language/reduce.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
export type ReduceCallback<T> = (
2-
accumulator: T,
1+
// I don't like TypeScript Anymore, my mood: 😔 -> 😩 -> 😭
2+
3+
export type ReduceCallback<T, U> = (
4+
accumulator: U,
35
value: T,
46
index: number,
57
array: readonly T[],
68
) => unknown;
79

8-
export function reduce<T, F extends ReduceCallback<T>>(
9-
array: T[],
10-
callback: F,
11-
initialValue?: unknown,
12-
) {
10+
export function reduce<
11+
T extends unknown[],
12+
F extends ReduceCallback<T, U>,
13+
U = unknown,
14+
>(array: readonly T[], callback: F, initialValue?: U | T) {
1315
const copy = [...array];
1416

15-
if (array.length >= 1) {
17+
if (array.length >= 1 && initialValue === undefined) {
1618
initialValue = copy.shift();
1719
}
1820

@@ -21,7 +23,7 @@ export function reduce<T, F extends ReduceCallback<T>>(
2123
const entries = copy.entries();
2224

2325
for (const [index, element] of entries) {
24-
previous = callback(previous as T, element, index, array);
26+
previous = callback(previous as T & U, element, index, array) as U;
2527
}
2628

2729
return previous as ReturnType<F>;

0 commit comments

Comments
 (0)