diff --git a/src/reduce.ts b/src/reduce.ts
index 24664c3e..52749f5d 100644
--- a/src/reduce.ts
+++ b/src/reduce.ts
@@ -72,8 +72,6 @@ async function async(
* {@link https://fxts.dev/docs/map | map}, {@link https://fxts.dev/docs/filter | filter}
*/
-function reduce(f: Arrow, iterable: A): undefined;
-
function reduce(f: Arrow, seed: B, iterable: A): B;
function reduce(f: (a: A, b: A) => A, iterable: Iterable): A;
@@ -113,11 +111,7 @@ function reduce | AsyncIterable, B>(
f: (a: B, b: IterableInfer) => B,
seed?: B | Iterable> | AsyncIterable>,
iterable?: Iterable> | AsyncIterable>,
-):
- | B
- | undefined
- | Promise
- | ((iterable: A) => ReturnValueType) {
+): B | Promise | ((iterable: A) => ReturnValueType) {
if (iterable === undefined) {
if (seed === undefined) {
return (iterable: A) =>
@@ -128,7 +122,7 @@ function reduce | AsyncIterable, B>(
const iterator = seed[Symbol.iterator]();
const { done, value } = iterator.next();
if (done) {
- return undefined;
+ throw new TypeError("'reduce' of empty iterable with no initial value");
}
return sync(f, value as B, {
[Symbol.iterator]() {
@@ -141,8 +135,11 @@ function reduce | AsyncIterable, B>(
const iterator = seed[Symbol.asyncIterator]();
return iterator.next().then(({ done, value }) => {
if (done) {
- return undefined;
+ throw new TypeError(
+ "'reduce' of empty iterable with no initial value",
+ );
}
+
return async(f, value as Promise, {
[Symbol.asyncIterator]() {
return iterator;
diff --git a/test/reduce.spec.ts b/test/reduce.spec.ts
index 3f0d4b97..5c1335ea 100644
--- a/test/reduce.spec.ts
+++ b/test/reduce.spec.ts
@@ -9,8 +9,8 @@ describe("reduce", function () {
expect(reduce((a, b) => a + b, "seed", [])).toEqual("seed");
});
- it("should return 'undefined' when the given `iterable` is an empty array and initial value is absent", function () {
- expect(reduce((a, b) => a + b, [])).toBeUndefined();
+ it("should be occured error when the given `iterable` is an empty array and initial value is absent", function () {
+ expect(() => reduce((a) => a, [])).toThrow();
});
it("should work given it is initial value", function () {
diff --git a/type-check/reduce.test.ts b/type-check/reduce.test.ts
index 53b85bbb..76bf2d20 100644
--- a/type-check/reduce.test.ts
+++ b/type-check/reduce.test.ts
@@ -3,7 +3,7 @@ import * as Test from "../src/types/Test";
const { checks, check } = Test;
-const res1 = reduce((a, b) => a + b, []);
+const res1 = reduce((a) => a, []);
const res2 = reduce((a, b) => a + b, "seed", []);
const res3 = reduce((a, b) => a + b, 0, [1, 2, 3]);
const res4 = reduce((a, b) => a + b, [1, 2, 3]);
@@ -52,7 +52,7 @@ const res16 = pipe(
);
checks([
- check(),
+ check(),
check(),
check(),
check(),