{"record":{"id":"85b08430265aec90","repo":"emberjs/ember.js","slug":"reduce-of-empty-array-with-no-initial-value","errorCode":null,"errorMessage":"Reduce of empty array with no initial value","messagePattern":"Reduce of empty array with no initial value","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/@ember/array/index.ts","lineNumber":1342,"sourceCode":"    let callback = iter(...arguments);\n    return any(this, callback);\n  },\n\n  // FIXME: When called without initialValue, behavior does not match native behavior\n  reduce<T, V>(\n    this: EmberArray<T>,\n    callback: (summation: V, current: T, index: number, arr: EmberArray<T>) => V,\n    initialValue?: V\n  ) {\n    assert('`reduce` expects a function as first argument.', typeof callback === 'function');\n\n    let hasInitialValue = arguments.length > 1;\n    let ret: any = initialValue;\n    let startIndex = 0;\n\n    if (!hasInitialValue) {\n      if (this.length === 0) {\n        throw new TypeError('Reduce of empty array with no initial value');\n      }\n      ret = this.objectAt(0);\n      startIndex = 1;\n    }\n\n    for (let i = startIndex; i < this.length; i++) {\n      let item = this.objectAt(i) as T;\n      ret = callback(ret, item, i, this);\n    }\n\n    return ret;\n  },\n\n  invoke<T>(this: EmberArray<T>, methodName: string, ...args: unknown[]) {\n    let ret = A();\n\n    // SAFETY: This is not entirely safe and the code will not work with Ember proxies\n    this.forEach((item: T) => ret.push((item as any)[methodName]?.(...args)));","sourceCodeStart":1324,"sourceCodeEnd":1360,"githubUrl":"https://github.com/emberjs/ember.js/blob/26f97246a8bf2e28edf26ac3093da2e86c04ffc5/packages/@ember/array/index.ts#L1324-L1360","documentation":"EmberArray#reduce mirrors native Array.reduce semantics: without an initial value it uses the first element as the seed. If the enumerable is empty and no initialValue argument was passed, there is no seed value, so it throws a TypeError.","triggerScenarios":"Calling someArray.reduce(fn) with exactly one argument on an empty Ember array (this.length === 0 and arguments.length <= 1 in the reduce implementation at packages/@ember/array/index.ts).","commonSituations":"Aggregating filtered results that can legitimately be empty (sums of filtered records, computed properties over empty lists).","solutions":["Pass an initial value: arr.reduce(fn, 0)","Check arr.length > 0 before reducing","Provide a default via computed/fallback logic"],"exampleFix":"// before\nlet total = items.reduce((sum, item) => sum + item.price, );\n// after\nlet total = items.reduce((sum, item) => sum + item.price, 0);","handlingStrategy":"validation","validationCode":"if (arr.length === 0) return 0; let total = arr.reduce((s, x) => s + x, 0);","typeGuard":"function isNonEmptyReducible(arr) { return Array.isArray(arr) && arr.length > 0; }","tryCatchPattern":"try { return arr.reduce(fn); } catch (e) { if (e instanceof TypeError && /no initial value/.test(e.message)) return undefined; throw e; }","preventionTips":["Always pass an initialValue to reduce","Handle empty filtered/queried collections explicitly","Prefer reduce(fn, seed) style in team lint rules"],"tags":["ember","array","reduce","empty-collection"],"backgroundTag":"reduce-of-empty-array","analyzedSha":"26f97246a8bf2e28edf26ac3093da2e86c04ffc5","analyzedAt":"2026-09-01T05:01:28.182Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}