discordjs/discord.js · error · TypeError
Reduce of empty collection with no initial value
Error message
Reduce of empty collection with no initial value
What it means
Collection.reduce() throws this TypeError when called with no initial value on a collection whose size is 0. With no initialValue, reduce needs at least one entry to seed the accumulator, so an empty collection makes it impossible to proceed. The library mirrors native Array.reduce behavior so callers get a familiar, fail-fast signal instead of a silently undefined result.
Source
Thrown at packages/collection/src/collection.ts:688
*/
public reduce(
fn: (accumulator: Value, value: Value, key: Key, collection: this) => Value,
initialValue?: Value,
): Value;
public reduce<InitialValue>(
fn: (accumulator: InitialValue, value: Value, key: Key, collection: this) => InitialValue,
initialValue: InitialValue,
): InitialValue;
public reduce<InitialValue>(
fn: (accumulator: InitialValue, value: Value, key: Key, collection: this) => InitialValue,
initialValue?: InitialValue,
): InitialValue {
if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`);
let accumulator!: InitialValue;
const iterator = this.entries();
if (initialValue === undefined) {
if (this.size === 0) throw new TypeError('Reduce of empty collection with no initial value');
accumulator = iterator.next().value![1] as unknown as InitialValue;
} else {
accumulator = initialValue;
}
for (const { 0: key, 1: value } of iterator) {
accumulator = fn(accumulator, value, key, this);
}
return accumulator;
}
/**
* Applies a function to produce a single value. Identical in behavior to
* {@link https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight | Array.reduceRight()}.
*
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `value`, `key`, and `collection`
* @param initialValue - Starting value for the accumulatorView on GitHub (pinned to a81ed8a306)
Solutions
- Pass an initial value as the second argument: collection.reduce((acc, v) => acc + v, 0).
- Check collection.size > 0 before calling reduce, or branch on size 0 to return a default.
- If reduce is inappropriate, use collection.first() with an emptiness check to seed manually.
- Ensure the collection is actually populated before aggregating (await fetches, verify filter didn't drop all entries).
Example fix
// before const total = collection.reduce((acc, v) => acc + v.points, 0); // after const total = collection.reduce((acc, v) => acc + v.points, 0); // pass 0 as initialValue
Defensive patterns
Strategy: validation
Validate before calling
if (!(collection instanceof Collection) || collection.size === 0) {
// handle empty case or use a default
}
// or simply always supply an initial value:
const total = collection.reduce((acc, v) => acc + v, 0); Try / catch
try {
total = collection.reduce((acc, v) => acc + v);
} catch (err) {
if (err instanceof TypeError && /empty collection/i.test(err.message)) total = 0;
else throw err;
} Prevention
- Never call reduce/reduceRight without an initial value on collections
- Check collection.size before aggregating
- Give reducers a sensible zero/identity default (0, '', {}, [])
- Remember fetches/filters can leave collections empty at runtime
When it happens
Trigger: Calling collection.reduce(fn) with one argument when the collection is empty (this.size === 0). Any reducer like sum(), result(), mapVarType/mapParam/astEntity helpers that aggregates values without passing an initialValue will throw if no entries exist.
Common situations: Aggregating values (sums, joined strings, merged results) from a collection filtered down to nothing; caching layers where the collection has not been populated yet (e.g. no guild members fetched); processing an empty result set from a database-backed Collection; forgetting that a previous clear()/sweep() emptied the collection.
Related errors
- The --tag option can only be used with --dev
- ${defaultValueGenerator} is not a function
- ${fn} is not a function
- InvalidType
- Unable to resolve body.
AI-assisted analysis of discordjs/discord.js@a81ed8a306 (2026-08-30).
Data as JSON: /api/errors/b302fba9dad0b12e.
Report an issue: GitHub.