{"record":{"id":"b302fba9dad0b12e","repo":"discordjs/discord.js","slug":"reduce-of-empty-collection-with-no-initial-value","errorCode":null,"errorMessage":"Reduce of empty collection with no initial value","messagePattern":"Reduce of empty collection with no initial value","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/collection/src/collection.ts","lineNumber":688,"sourceCode":"\t */\n\tpublic reduce(\n\t\tfn: (accumulator: Value, value: Value, key: Key, collection: this) => Value,\n\t\tinitialValue?: Value,\n\t): Value;\n\tpublic reduce<InitialValue>(\n\t\tfn: (accumulator: InitialValue, value: Value, key: Key, collection: this) => InitialValue,\n\t\tinitialValue: InitialValue,\n\t): InitialValue;\n\tpublic reduce<InitialValue>(\n\t\tfn: (accumulator: InitialValue, value: Value, key: Key, collection: this) => InitialValue,\n\t\tinitialValue?: InitialValue,\n\t): InitialValue {\n\t\tif (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`);\n\t\tlet accumulator!: InitialValue;\n\n\t\tconst iterator = this.entries();\n\t\tif (initialValue === undefined) {\n\t\t\tif (this.size === 0) throw new TypeError('Reduce of empty collection with no initial value');\n\t\t\taccumulator = iterator.next().value![1] as unknown as InitialValue;\n\t\t} else {\n\t\t\taccumulator = initialValue;\n\t\t}\n\n\t\tfor (const { 0: key, 1: value } of iterator) {\n\t\t\taccumulator = fn(accumulator, value, key, this);\n\t\t}\n\n\t\treturn accumulator;\n\t}\n\n\t/**\n\t * Applies a function to produce a single value. Identical in behavior to\n\t * {@link https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight | Array.reduceRight()}.\n\t *\n\t * @param fn - Function used to reduce, taking four arguments; `accumulator`, `value`, `key`, and `collection`\n\t * @param initialValue - Starting value for the accumulator","sourceCodeStart":670,"sourceCodeEnd":706,"githubUrl":"https://github.com/discordjs/discord.js/blob/a81ed8a306d37fdc746e26a634b6a42164ba2c8c/packages/collection/src/collection.ts#L670-L706","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)."],"exampleFix":"// before\nconst total = collection.reduce((acc, v) => acc + v.points, 0);\n// after\nconst total = collection.reduce((acc, v) => acc + v.points, 0); // pass 0 as initialValue","handlingStrategy":"validation","validationCode":"if (!(collection instanceof Collection) || collection.size === 0) {\n  // handle empty case or use a default\n}\n// or simply always supply an initial value:\nconst total = collection.reduce((acc, v) => acc + v, 0);","typeGuard":null,"tryCatchPattern":"try {\n  total = collection.reduce((acc, v) => acc + v);\n} catch (err) {\n  if (err instanceof TypeError && /empty collection/i.test(err.message)) total = 0;\n  else throw err;\n}","preventionTips":["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"],"tags":["typeerror","empty-collection","reduce","arguments"],"backgroundTag":"reduce-empty-collection-no-initial-value","analyzedSha":"a81ed8a306d37fdc746e26a634b6a42164ba2c8c","analyzedAt":"2026-08-30T04:07:22.193Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}